description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
inp = [int(x) for x in input().split()]
l = {}
ans = 0
for i in range(n):
x = inp[i]
ans += x * (2 * i - n + 1)
if x - 1 in l:
ans -= l[x - 1]
if x + 1 in l:
ans += l[x + 1]
l[x] = l[x] + 1 if x in l else 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | cnt = {}
n = int(input())
s = 0
an = 0
a = list(map(int, input().split()))
for i in range(n):
if a[i] not in cnt.keys():
cnt[a[i]] = 0
if a[i] - 1 not in cnt.keys():
cnt[a[i] - 1] = 0
if a[i] + 1 not in cnt.keys():
cnt[a[i] + 1] = 0
s += a[i]
cnt[a[i]] += 1
num = i + 1 - cnt[a[i]] - cnt[a[i] - 1] - cnt[a[i] + 1]
an += num * a[i] - (
s - a[i] * cnt[a[i]] - (a[i] + 1) * cnt[a[i] + 1] - (a[i] - 1) * cnt[a[i] - 1]
)
print(an) | ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
a = list(map(int, input().split()))
ps = a[0]
ans = 0
h = {a[0]: 1}
hv = {a[0]: a[0]}
def gv(h, k):
if k in h:
return h[k]
else:
return 0
for i in range(1, n):
c = i - gv(h, a[i]) - gv(h, a[i] - 1) - gv(h, a[i] + 1)
cps = ps - gv(hv, a[i]) - gv(hv, a[i] - 1) - gv(hv, a[i] + 1)
ans += a[i] * c - cps
if a[i] not in h:
h[a[i]] = 0
hv[a[i]] = 0
h[a[i]] += 1
hv[a[i]] += a[i]
ps += a[i]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR DICT VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
arr = list(map(int, input().split()))
summ = sum(arr)
ans = 0
for i in range(n - 1):
ans += summ - arr[i] - (n - i - 1) * arr[i]
summ -= arr[i]
d = {}
for i in range(n):
if arr[i] in d.keys():
d[arr[i]] += 1
else:
d[arr[i]] = 1
for i in range(n):
if arr[i] - 1 in d.keys():
ans += d[arr[i] - 1]
if arr[i] + 1 in d.keys():
ans -= d[arr[i] + 1]
d[arr[i]] -= 1
print(ans) | 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 VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
a = list(map(int, input().split()))
d1 = dict()
s = 0
for i in range(n):
d1[a[i]] = d1.get(a[i], 0) + 1
s += a[i]
p = 0
ans = 0
d2 = dict()
for i in range(n):
d2[a[i]] = d2.get(a[i], 0) + 1
p += a[i]
k1 = d1.get(a[i] - 1, 0) - d2.get(a[i] - 1, 0)
k2 = d1.get(a[i], 0) - d2.get(a[i], 0)
k3 = d1.get(a[i] + 1, 0) - d2.get(a[i] + 1, 0)
k = n - i - 1 - k1 - k2 - k3
ans += s - p - (a[i] - 1) * k1 - a[i] * k2 - (a[i] + 1) * k3 - k * a[i]
print(ans) | 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | import sys
N = int(input())
a = []
b = {}
ans = 0
a = input().split()
for i in range(N):
b[int(a[i])] = 0
b[int(a[i]) - 1] = 0
b[int(a[i]) + 1] = 0
for i in range(N):
ans += int(a[i]) * (i - (N - 1 - i)) - (b[int(a[i]) - 1] - b[int(a[i]) + 1])
b[int(a[i])] = b[int(a[i])] + 1
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | def main():
n = int(input())
array = list(map(int, input().split()))
counts = dict()
for elem in array:
counts[elem - 1], counts[elem], counts[elem + 1] = 0, 0, 0
answer, l, r = 0, 0, n - 1
for elem in array:
counts[elem] += 1
answer += (l - r) * elem
l += 1
r -= 1
for elem in array:
counts[elem] -= 1
answer += counts[elem - 1] - counts[elem + 1]
print(answer)
main() | FUNC_DEF 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 FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
a = list(map(int, input().split()))
sum0 = 0
ans = 0
sum1 = 0
m = {}
for i in range(0, n):
m[a[i]] = 0
m[a[i] + 1] = 0
m[a[i] - 1] = 0
sum0 += a[i]
for i in range(0, n):
m[a[i]] += 1
for i in range(n - 1, -1, -1):
sum1 = sum0 - a[i] - m[a[i] - 1] * (a[i] - 1) - m[a[i] + 1] * (a[i] + 1)
x = (n - m[a[i] - 1] - m[a[i] + 1] - 1) * a[i]
m[a[i]] -= 1
n -= 1
sum0 -= a[i]
ans += x - sum1
print(ans) | 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 ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
arr = [int(e) for e in input().split()]
m = {}
pref = 0
ans = 0
for i in arr:
m[i] = 0
m[i + 1] = 0
m[i - 1] = 0
for i in range(len(arr)):
ans += i * arr[i] - pref
ans -= m[arr[i] - 1] - m[arr[i] + 1]
pref += arr[i]
m[arr[i]] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
a = list(map(int, input().split()))
d = {}
ans = 0
s = 0
for z in range(n):
i = a[z]
if i not in d:
d[i] = 0
d[i] += 1
count = d[i] + (d[i - 1] if i - 1 in d else 0) + (d[i + 1] if i + 1 in d else 0)
tot = (
d[i] * i
+ (d[i - 1] if i - 1 in d else 0) * (i - 1)
+ (d[i + 1] if i + 1 in d else 0) * (i + 1)
)
s += i
ans += i * (z - count + 1) - (s - tot)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
ans = 0
s = {}
for i, a in enumerate(map(int, input().split())):
ans += a * i - a * (n - 1 - i) + s.get(a + 1, 0) - s.get(a - 1, 0)
s[a] = s.get(a, 0) + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n_elements = int(input())
elements = [int(x) for x in input().split()]
res = 0
for i, x in enumerate(elements):
res -= (n_elements - 1 - i) * x
for i, y in enumerate(elements):
res += i * y
m = dict()
m[elements[0]] = 1
for num in elements[1:]:
if num + 1 in m:
res += m[num + 1]
if num - 1 in m:
res -= m[num - 1]
if num in m:
m[num] += 1
else:
m[num] = 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
a = input().split(" ")
for i in range(n):
a[i] = int(a[i])
ans = 0
sum = 0
m = dict()
for i in range(n):
m[a[i]] = m.get(a[i], 0) + 1
sum += a[i]
ans += (i + 1) * a[i] - sum
ans -= m.get(a[i] + 1, 0) * -1
ans -= m.get(a[i] - 1, 0)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
l = list(map(int, input().split()))
d = {}
ans = 0
curr = 0
for i in range(len(l) - 1, -1, -1):
curr += l[i]
z = 0
z1 = 0
if l[i] - 1 in d:
z = d[l[i] - 1]
else:
z = 0
if l[i] + 1 in d:
z1 = d[l[i] + 1]
else:
z1 = 0
ans += curr - (len(l) - i) * l[i] + z - z1
if l[i] in d:
d[l[i]] += 1
else:
d[l[i]] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
l = input().split()
l = [int(i) for i in l]
cnt = {}
for i in l:
cnt[i - 1] = 0
cnt[i + 1] = 0
cnt[i] = 0
ans = 0
pre = 0
for i in range(0, n):
ans += l[i] * i - pre
pre += l[i]
ans -= cnt[l[i] - 1]
ans += cnt[l[i] + 1]
cnt[l[i]] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
res = 0
sum = 0
qua = 0
m = dict()
a = list(map(int, input().split()))
for i in range(0, n):
if m.get(a[i] - 1) == None:
m[a[i] - 1] = 0
if m.get(a[i] + 1) == None:
m[a[i] + 1] = 0
if m.get(a[i]) == None:
m[a[i]] = 0
res += a[i] * (qua - m[a[i] - 1] - m[a[i]] - m[a[i] + 1]) - (
sum - (a[i] - 1) * m[a[i] - 1] - a[i] * m[a[i]] - (a[i] + 1) * m[a[i] + 1]
)
sum += a[i]
qua += 1
m[a[i]] += 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NONE ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NONE ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | tcase = int(input().strip())
li = list(map(lambda x: int(x), input().split()))
ans = 0
sum = li[0]
di = {}
di[sum] = 1
i = 1
for x in li[1:]:
if x + 1 in di:
df = di[x + 1]
else:
df = 0
if x - 1 in di:
da = di[x - 1]
else:
da = 0
ans += i * x - sum + df - da
sum += x
i += 1
if x in di:
di[x] += 1
else:
di[x] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
arr = [int(e) for e in input().split()]
m = {}
f = {}
for i in arr:
m[i] = 0
m[i - 1] = 0
m[i + 1] = 0
f[i] = 0
f[i - 1] = 0
f[i + 1] = 0
for i in arr:
m[i] += 1
ans = 0
for i in range(n):
tot = n
left = i
right = n - i - 1
left -= f[arr[i]]
left -= f[arr[i] - 1]
left -= f[arr[i] + 1]
f[arr[i]] += 1
right -= m[arr[i]] - f[arr[i]]
right -= m[arr[i] + 1] - f[arr[i] + 1]
right -= m[arr[i] - 1] - f[arr[i] - 1]
ans += left * arr[i] - right * arr[i]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
M = {}
F = [0] * 200005
P = [0] * 200005
F = list(map(int, input().split()))
P[0] = F[0]
for i in range(n):
val = M.get(F[i])
if val == None:
val = 0
M[F[i]] = val + 1
P[i] = P[i - 1] + F[i]
ans = 0
for i in range(n):
val = M.get(F[i])
M[F[i]] = val - 1
ans += P[n - 1] - P[i] - (n - 1 - i) * F[i]
val = M.get(F[i] + 1)
if val == None:
val = 0
ans -= val
val = M.get(F[i] - 1)
if val == None:
val = 0
ans += val
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
all = list(input().split(" "))
qtd = 0
cur = 0
ans = 0
cnt = dict()
for num in all:
x = int(num)
qtd = qtd + 1
ans = ans + (qtd - 1) * x - cur
if x - 1 in cnt:
ans = ans - cnt[x - 1]
if x + 1 in cnt:
ans = ans + cnt[x + 1]
cur = cur + x
if x in cnt:
cnt[x] = cnt[x] + 1
else:
cnt[x] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = input()
ar = list(map(int, input().split()))
sum = 0
pref = {}
for i in ar:
pref[i] = 0
pref[i - 1] = 0
pref[i + 1] = 0
ans = 0
k = 0
for i in ar:
ans += k * i - sum - pref[i - 1] + pref[i + 1]
pref[i] += 1
sum += i
k += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | from sys import stdin, stdout
n = int(stdin.readline())
a = [int(i) for i in stdin.readline().split()]
d = dict()
ans, sm = 0, 0
for i in range(n):
if a[i] not in d.keys():
d[a[i]] = 0
d[a[i]] += 1
ans += i * a[i] - sm
if a[i] + 1 in d.keys():
ans += 1 * d[a[i] + 1]
if a[i] - 1 in d.keys():
ans -= 1 * d[a[i] - 1]
sm += a[i]
stdout.write(str(ans)) | 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 ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | inp = []
d = {}
n = int(input())
tot = 0
ans = 0
temp = input()
temp = temp.split()
inp = [int(i) for i in temp]
for i in range(n):
val = inp[i]
tot = tot + val
if val not in d:
d[val] = 1
else:
d[val] += 1
for i in range(n - 1, 0, -1):
prev = 0
nxt = 0
if inp[i] + 1 in d:
nxt = d[inp[i] + 1]
if inp[i] - 1 in d:
prev = d[inp[i] - 1]
ans += inp[i] * (i + 1 - d[inp[i]] - prev - nxt)
ans -= tot - inp[i] * d[inp[i]] - (inp[i] + 1) * nxt - (inp[i] - 1) * prev
d[inp[i]] -= 1
tot -= inp[i]
print(ans) | ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | n = int(input())
v = list(map(int, input().split()))
m = {}
res = 0
for i in v:
if not i in m:
m[i] = 0
m[i] += 1
k = v.copy()
for i in range(n - 1, 0, -1):
k[i - 1] = k[i] + k[i - 1]
for i in range(0, n - 1):
res += -(n - i - 1) * v[i] + k[i + 1]
m[v[i]] -= 1
if v[i] + 1 in m:
res -= m[v[i] + 1]
if v[i] - 1 in m:
res += m[v[i] - 1]
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Input-----
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β elements of the array.
-----Output-----
Print one integer β the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
-----Examples-----
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
-----Note-----
In the first example:
d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2. | t = int(input())
a = input().split(" ")
sum = []
curr = 0
dic = {}
sum.append(0)
for i in a:
curr = curr + int(i)
sum.append(curr)
if i in dic:
p = dic[i]
p = p + 1
dic[i] = p
else:
dic[i] = 1
ans = 0
for i in range(1, t + 1):
ans = ans + sum[t] - sum[i] - int(t - int(i)) * int(a[i - 1])
p1 = str(int(a[i - 1]) + 1)
p2 = str(int(a[i - 1]) - 1)
if str(p1) in dic:
ans = ans - int(dic[str(int(a[i - 1]) + 1)])
if str(p2) in dic:
ans = ans + int(dic[str(int(a[i - 1]) - 1)])
p = dic[a[i - 1]]
p = p - 1
dic[a[i - 1]] = p
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Given a matrix containing lower alphabetical characters only of size n*m. We need to count the number of palindromic paths in the given matrix.
A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell.
Example 1:
Input: matrix = {{a,a,a,b},{b,a,a,a},{a,b,b,a}}
Output: 3
Explanation: Number of palindromic paths are 3
from top-left to bottom-right.
aaaaaa (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) ->
(1, 3) -> (2, 3)
aaaaaa (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) ->
(1, 3) -> (2, 3)
abaaba (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) ->
(2, 2) -> (2, 3)
Example 2:
Input: matrix = {{a,b},{c,d}}
Output: 0
Explanation: There is no palindromic paths.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function countPalindromicPaths() which takes the matrix as input parameter and returns the total nuumber of palindromic paths modulo 10^{9} + 7.
Expected Time Complexity: O(n^{2}*m^{2})
Space Complexity: O(n*m)
Constraints:
1 β€ n, m β€ 100 | class Solution:
def countOfPalindromicPaths(self, matrix):
mp = {}
M = 1000000007
def findP(arr, i, j, p, q, n, m):
n = len(arr)
m = len(arr[0])
if i >= n or j >= m or p < 0 or q < 0:
return 0
if arr[i][j] != arr[p][q]:
return 0
if (
i == p
and j == q
or abs(i - p) == 1
and j - q == 0
or i - p == 0
and abs(j - q) == 1
):
return 1
if i > p or j > q:
return 0
curr = str(i) + "&" + str(j) + "&" + str(p) + "&" + str(q)
if curr in mp:
return mp[curr]
op1 = findP(arr, i, j + 1, p, q - 1, n, m)
op2 = findP(arr, i, j + 1, p - 1, q, n, m)
op3 = findP(arr, i + 1, j, p, q - 1, n, m)
op4 = findP(arr, i + 1, j, p - 1, q, n, m)
mp[curr] = (op1 + op2 + op3 + op4) % M
return mp[curr]
n = len(matrix)
m = len(matrix[-1])
return findP(matrix, 0, 0, n - 1, m - 1, n, m) % M | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR |
Given a matrix containing lower alphabetical characters only of size n*m. We need to count the number of palindromic paths in the given matrix.
A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell.
Example 1:
Input: matrix = {{a,a,a,b},{b,a,a,a},{a,b,b,a}}
Output: 3
Explanation: Number of palindromic paths are 3
from top-left to bottom-right.
aaaaaa (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) ->
(1, 3) -> (2, 3)
aaaaaa (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) ->
(1, 3) -> (2, 3)
abaaba (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) ->
(2, 2) -> (2, 3)
Example 2:
Input: matrix = {{a,b},{c,d}}
Output: 0
Explanation: There is no palindromic paths.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function countPalindromicPaths() which takes the matrix as input parameter and returns the total nuumber of palindromic paths modulo 10^{9} + 7.
Expected Time Complexity: O(n^{2}*m^{2})
Space Complexity: O(n*m)
Constraints:
1 β€ n, m β€ 100 | class Solution:
def __init__(self):
self.mod = 10**9 + 7
self.dp = {}
def countOfPalindromicPaths(self, matrix):
return self.path(matrix, 0, 0, len(matrix) - 1, len(matrix[0]) - 1) % self.mod
def path(self, matrix, i, j, x, y):
if (
i >= len(matrix)
or j >= len(matrix[0])
or x < 0
or y < 0
or matrix[i][j] != matrix[x][y]
):
return 0
if i == x and j == y or abs(i - x) + abs(j - y) <= 1:
return 1
if i > x or j > y:
return 0
if (i, j, x, y) in self.dp:
return self.dp[i, j, x, y]
op1 = self.path(matrix, i + 1, j, x - 1, y) % self.mod
op2 = self.path(matrix, i + 1, j, x, y - 1) % self.mod
op3 = self.path(matrix, i, j + 1, x - 1, y) % self.mod
op4 = self.path(matrix, i, j + 1, x, y - 1) % self.mod
temp = (op1 + op2 + op3 + op4) % self.mod
self.dp[i, j, x, y] = temp
return temp | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR |
Given a matrix containing lower alphabetical characters only of size n*m. We need to count the number of palindromic paths in the given matrix.
A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell.
Example 1:
Input: matrix = {{a,a,a,b},{b,a,a,a},{a,b,b,a}}
Output: 3
Explanation: Number of palindromic paths are 3
from top-left to bottom-right.
aaaaaa (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) ->
(1, 3) -> (2, 3)
aaaaaa (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) ->
(1, 3) -> (2, 3)
abaaba (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) ->
(2, 2) -> (2, 3)
Example 2:
Input: matrix = {{a,b},{c,d}}
Output: 0
Explanation: There is no palindromic paths.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function countPalindromicPaths() which takes the matrix as input parameter and returns the total nuumber of palindromic paths modulo 10^{9} + 7.
Expected Time Complexity: O(n^{2}*m^{2})
Space Complexity: O(n*m)
Constraints:
1 β€ n, m β€ 100 | class Solution:
def countOfPalindromicPaths(self, arr):
n = len(arr)
m = len(arr[0])
memo = {}
def pali(s, e):
global count
x1, y1 = s
x2, y2 = e
if x1 > x2 or y1 > y2:
return 0
if x1 == x2 and y1 == y2:
return 1
elif x2 == x1 + 1 and y1 == y2 or x1 == x2 and y2 == y1 + 1:
return 1
if (x1, x2, y1, y2) in memo:
return memo[x1, x2, y1, y2]
a1 = 0
a2 = 0
a3 = 0
a4 = 0
if (x1 <= x2 - 1 and y1 + 1 <= y2) and arr[x1][y1 + 1] == arr[x2 - 1][y2]:
a1 = pali([x1, y1 + 1], [x2 - 1, y2])
if (x1 <= x2 and y1 + 1 <= y2 - 1) and arr[x1][y1 + 1] == arr[x2][y2 - 1]:
a2 = pali([x1, y1 + 1], [x2, y2 - 1])
if (x1 + 1 <= x2 and y1 <= y2 - 1) and arr[x1 + 1][y1] == arr[x2][y2 - 1]:
a3 = pali([x1 + 1, y1], [x2, y2 - 1])
if (x1 + 1 <= x2 - 1 and y1 <= y2) and arr[x1 + 1][y1] == arr[x2 - 1][y2]:
a4 = pali([x1 + 1, y1], [x2 - 1, y2])
memo[x1, x2, y1, y2] = a1 + a2 + a3 + a4
return memo[x1, x2, y1, y2]
count = 0
if arr[0][0] == arr[n - 1][m - 1]:
count = pali([0, 0], [n - 1, m - 1])
return count % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a matrix containing lower alphabetical characters only of size n*m. We need to count the number of palindromic paths in the given matrix.
A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell.
Example 1:
Input: matrix = {{a,a,a,b},{b,a,a,a},{a,b,b,a}}
Output: 3
Explanation: Number of palindromic paths are 3
from top-left to bottom-right.
aaaaaa (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) ->
(1, 3) -> (2, 3)
aaaaaa (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) ->
(1, 3) -> (2, 3)
abaaba (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) ->
(2, 2) -> (2, 3)
Example 2:
Input: matrix = {{a,b},{c,d}}
Output: 0
Explanation: There is no palindromic paths.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function countPalindromicPaths() which takes the matrix as input parameter and returns the total nuumber of palindromic paths modulo 10^{9} + 7.
Expected Time Complexity: O(n^{2}*m^{2})
Space Complexity: O(n*m)
Constraints:
1 β€ n, m β€ 100 | class Solution:
def countOfPalindromicPaths(self, matrix):
m = len(matrix)
n = len(matrix[0])
memo = {}
count = self.helper(matrix, 0, 0, m - 1, n - 1, m, n, memo)
return count
def helper(self, matrix, t_i, t_j, d_i, d_j, m, n, memo):
if t_i < 0 or t_i > m - 1 or t_j < 0 or t_j > n - 1:
return 0
if d_i < 0 or d_i > m - 1 or d_j < 0 or d_j > n - 1:
return 0
if t_i > d_i or t_j > d_j:
return 0
if (t_i, t_j, d_i, d_j) in memo:
return memo[t_i, t_j, d_i, d_j]
if matrix[t_i][t_j] != matrix[d_i][d_j]:
return 0
if (
t_i == d_i
and t_j == d_j
or t_i + 1 == d_i
and t_j == d_j
or t_i == d_i
and t_j + 1 == d_j
):
return 1
count = 0
count += self.helper(matrix, t_i, t_j + 1, d_i - 1, d_j, m, n, memo)
count += self.helper(matrix, t_i, t_j + 1, d_i, d_j - 1, m, n, memo)
count += self.helper(matrix, t_i + 1, t_j, d_i - 1, d_j, m, n, memo)
count += self.helper(matrix, t_i + 1, t_j, d_i, d_j - 1, m, n, memo)
count = count % (10**9 + 7)
memo[t_i, t_j, d_i, d_j] = count
return memo[t_i, t_j, d_i, d_j] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR |
Given a matrix containing lower alphabetical characters only of size n*m. We need to count the number of palindromic paths in the given matrix.
A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell.
Example 1:
Input: matrix = {{a,a,a,b},{b,a,a,a},{a,b,b,a}}
Output: 3
Explanation: Number of palindromic paths are 3
from top-left to bottom-right.
aaaaaa (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) ->
(1, 3) -> (2, 3)
aaaaaa (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) ->
(1, 3) -> (2, 3)
abaaba (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) ->
(2, 2) -> (2, 3)
Example 2:
Input: matrix = {{a,b},{c,d}}
Output: 0
Explanation: There is no palindromic paths.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function countPalindromicPaths() which takes the matrix as input parameter and returns the total nuumber of palindromic paths modulo 10^{9} + 7.
Expected Time Complexity: O(n^{2}*m^{2})
Space Complexity: O(n*m)
Constraints:
1 β€ n, m β€ 100 | class Solution:
def solve(self, ma, i1, j1, i2, j2, dp):
n = len(ma)
m = len(ma[0])
if (
i1 >= n
or j1 >= m
or i2 < 0
or j2 < 0
or i1 > i2
or j1 > j2
or ma[i1][j1] != ma[i2][j2]
):
return 0
if (
i1 == i2
and j1 == j2
or i1 == i2
and j1 + 1 == j2
or j1 == j2
and i1 + 1 == i2
):
return 1
x = i1 * n + j1
y = i2 * n + j2
if x in dp and y in dp[x]:
return dp[x][y]
ans = 0
ans = (ans + self.solve(ma, i1 + 1, j1, i2 - 1, j2, dp)) % self.M
ans = (ans + self.solve(ma, i1 + 1, j1, i2, j2 - 1, dp)) % self.M
ans = (ans + self.solve(ma, i1, j1 + 1, i2 - 1, j2, dp)) % self.M
ans = (ans + self.solve(ma, i1, j1 + 1, i2, j2 - 1, dp)) % self.M
if not x in dp:
dp[x] = {}
if not y in dp[x]:
dp[x][y] = ans
return ans
def countOfPalindromicPaths(self, matrix):
dp = {}
n = len(matrix)
for i in range(n):
if matrix[i][-1] == " ":
matrix[i].pop()
m = len(matrix[0])
self.M = int(1000000000.0 + 7)
return self.solve(matrix, 0, 0, n - 1, m - 1, dp) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
for tt in range(t):
n = int(input())
nlist = list(map(int, input().split()))
count = 0
for i in range(len(nlist)):
if i >= nlist[i] - 1:
count += n - i
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | def solve(arr):
result = 0
for i, val in enumerate(arr, 1):
if val > i:
continue
result += len(arr) - i + 1
return result
def main():
for _ in range(int(input())):
N = int(input())
arr = [int(val) for val in input().split()]
result = solve(arr)
print(result)
main() | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | T = int(input())
for o in range(T):
n = int(input())
l = list(map(int, input().split()))
c = 0
for j in range(n):
if l[j] == j + 1:
c = c + n - j
elif l[j] < j + 1:
c = c + n - j
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | try:
for i in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()]
c = 0
for i in range(n):
if a[i] in range(1, i + 2):
c += n - i
print(c)
except EOFError:
print(0) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | for t in range(int(input())):
n = int(input())
sm = 0
liz = list(map(int, input().split()))
for i in range(0, n):
if i + 1 >= liz[i]:
sm = sm + 1
sm = sm + (n - (i + 1))
print(sm) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
for i in range(0, t):
n = int(input())
count = 0
l = [int(i) for i in input().split()]
l_2 = []
for i in range(0, n):
l_2.append([l[i], i + 1])
for i in range(0, n):
if l_2[i][0] <= l_2[i][1]:
count += n - i
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | for _ in range(int(input())):
n = int(input())
c = 0
A = list(map(int, input().split()))
for i in range(n):
if i + 1 >= A[i]:
c += n - i
print(c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | def fun(n, l):
c = 0
for i in range(n):
k = i + 1
if k >= l[i]:
c += n - k + 1
return c
for t in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
print(fun(n, l)) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
for z in range(t):
count = 0
n = int(input())
l = list(map(int, input().split()))
for i in range(n):
if l[i] in range(1, i + 2):
count += n - i
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | def main():
for _ in range(int(input())):
input()
ans = 0
arr = [int(i) for i in input().split()]
for n in range(len(arr)):
if n + 1 >= arr[n]:
ans += len(arr) - (n + 1) + 1
print(ans)
main() | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | for _ in range(int(input())):
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
if A[i] == i + 1:
ans += N - i
if A[i] <= i:
ans += N - i
print(ans) | 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 NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
while t > 0:
n = int(input())
ar = list(map(int, input().split()))
dp = [0] * n
if ar[0] == 1:
dp[0] = 1
for i in range(1, n):
if ar[i] <= i + 1:
dp[i] += dp[i - 1] + 1
else:
dp[i] = dp[i - 1]
s = 0
for i in dp:
s += i
print(s)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 LIST NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
for _ in range(0, t):
n = int(input())
count = 0
arr = list(map(int, input().split()))
for i in range(0, n):
if arr[i] == 1:
count += n - i
continue
if arr[i] <= i + 1:
count += n - i
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR 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 FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | try:
test_cases = int(input())
for i in range(0, test_cases):
count = 0
l = int(input())
A = input().split()
for i in range(0, l):
number = int(A[i])
index = i + 1
if index >= number:
count = count + l - index + 1
print(count)
except:
pass | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
while t:
n = int(input())
l = list(map(int, input().split()))
c = 0
for i in range(1, n + 1):
if i == l[i - 1]:
c += n - i + 1
elif i - l[i - 1] > 0:
c += n - i + 1
print(c)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(1):
b = a[i:n]
for j in range(len(b)):
if b[j] == j + 1:
ans += 1 * (len(b) - j)
elif b[j] <= j + 1:
ans += 1 * (len(b) - j)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
while t > 0:
t -= 1
n = int(input())
a = [int(x) for x in input().split()]
ans = 0
for i in range(n):
if a[i] > i + 1:
continue
else:
ans += n - i
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | T = int(input())
for i in range(T):
N = int(input())
arr = map(int, input().split())
ss = 0
for idx, n in enumerate(arr):
if idx + 1 >= n:
ss += N - (idx + 1) + 1
print(ss) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | import sys
input = sys.stdin.readline
t = int(input())
while t > 0:
n = int(input())
arr = list(map(int, input().split()))
count = 0
for i in range(0, n):
if arr[i] <= i + 1:
count += n - i
print(count)
t -= 1 | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
for _ in range(t):
n = int(input())
arr = input()
arr = arr.split(" ")
ans = 0
for i in range(1, len(arr) + 1):
val = int(arr[i - 1])
if i - val + 1 > 0:
ans += n - i + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | def main():
for t in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
total = 0
new = []
for i, j in enumerate(arr, start=1):
if i >= j:
new.append(total + 1)
total += 1
else:
new.append(total)
print(sum(new))
main() | FUNC_DEF 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 NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | try:
t = int(input())
for i in range(0, t):
n = int(input())
g = input()
arr = list(map(int, g.split()))
fp = 0
p = 0
for i in range(1, n + 1):
if i == arr[i - 1]:
p = n - i + 1
fp = fp + p
if i > arr[i - 1]:
p = n - i + 1
fp = fp + p
print(fp)
except:
pass | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
print(sum([(n - k) for k, v in enumerate(l) if v <= k + 1])) | 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 EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | for _ in range(int(input())):
a = int(input())
b = [int(i) for i in input().split()]
sum = 0
for i in range(1, 1 + a):
if i >= b[i - 1]:
sum += a - i + 1
print(sum) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | for _ in range(int(input())):
n = int(input())
l = [0]
l.extend(map(int, input().split()))
ref = list(map(int, "0" * (n + 1)))
sum = 0
for i in range(1, n + 1):
sum += (i - l[i] + 1 > 0) * (n - i + 1)
print(sum) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.
An index of an array is called a fixed point if the value on the index of the array is equal to the index itself. Formally, an index i of an array A of length N is a fixed point if A_{i} = i, where 1β€ i β€ N.
For example, in the array A = [1, 3, 3, 4, 1], the indices 1, 3, 4 are fixed points.
Chef has an array A consisting of N integers. Chef tries to find the number of fixed points over all subarrays of the array A. More formally, for every pair (L, R) such that 1 β€ L β€ R β€ N, Chef considers the subarray A_{L}, A_{L+1}, \dots, A_{R} and calculates the total number of fixed points in all such subarrays. However, Chef finds this task very hard. Can you help him find the number of fixed points over all subarrays?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N, denoting the length of the array.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the given array.
------ Output Format ------
For each test case, print a single line containing one integer - the number of fixed points over all subarrays.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test caes does not exceed $5\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2
1 2
3
2 3 2
4
1 3 4 2
----- Sample Output 1 ------
3
1
5
----- explanation 1 ------
Let, $A[L, R]$ denotes the subarray consisting of integers from index $L$ to index $R$ of the array $A$(i.e $A_{L}, A_{L+1}, \dots, A_{R}$).
Test case $1$:
- In the subarray $A[1, 1] = [1]$, the number of fixed points is $1$.
- In the subarray $A[1, 2] = [1, 2]$, the number of fixed points is $2$.
- In the subarray $A[2, 2] = [2]$, there is no fixed point.
Hence the total fixed points over all subarrays $= 1 + 2 + 0 = 3$.
Test case $2$: In the subarray $A[2, 3] = [3, 2]$, the second index is a fixed point. There is no fixed point in any other subarrays. | t = int(input())
while t > 0:
n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(0, n):
if i + 1 == a[i]:
count += n - i
elif a[i] < i + 1:
count += n - i
print(count)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n == 1:
print("1")
print("1")
if n == 2:
print("1 2")
print("2 1")
if n == 3:
print("1 3 2")
print("3 1 2")
if n == 4:
print("2 1 4 3")
print("4 1 2 3")
if n == 5:
print("2 1 4 5 3")
print("5 1 2 3 4")
if n == 6:
print("2 3 1 5 6 4")
print("6 1 2 3 4 5")
if n == 7:
print("2 3 1 5 6 7 4")
print("7 1 2 3 4 5 6")
if n == 8:
print("2 3 4 1 6 7 8 5")
print("8 1 2 3 4 5 6 7")
if n == 9:
print("2 3 4 1 6 7 8 9 5")
print("9 1 2 3 4 5 6 7 8")
if n == 10:
print("2 3 4 5 1 7 8 9 10 6")
print("10 1 2 3 4 5 6 7 8 9")
if n == 11:
print("2 3 4 5 1 7 8 9 10 11 6")
print("11 1 2 3 4 5 6 7 8 9 10")
if n == 12:
print("2 3 4 5 6 1 8 9 10 11 12 7")
print("12 1 2 3 4 5 6 7 8 9 10 11")
if n == 13:
print("2 3 4 5 6 1 8 9 10 11 12 13 7")
print("13 1 2 3 4 5 6 7 8 9 10 11 12")
if n == 14:
print("2 3 4 5 6 7 1 9 10 11 12 13 14 8")
print("14 1 2 3 4 5 6 7 8 9 10 11 12 13")
if n == 15:
print("2 3 4 5 6 7 1 9 10 11 12 13 14 15 8")
print("15 1 2 3 4 5 6 7 8 9 10 11 12 13 14")
if n == 16:
print("2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 9")
print("16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15")
if n == 17:
print("2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 17 9")
print("17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | N = int(input())
mn = [N]
for i in range(1, N):
mn.append(i)
mx = [1]
for i in range(2, N + 1):
mx.append(i)
mid = int(N / 2)
mx[0], mx[mid] = mx[mid], mx[0]
for i in range(N - 1):
if i != mid or i + 1 != mid:
mx[i], mx[i + 1] = mx[i + 1], mx[i]
MX = ""
MN = ""
for i in range(N):
MX = MX + " " + str(mx[i])
MN = MN + " " + str(mn[i])
print(MX)
print(MN) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n == 1:
print(1)
print(1)
exit()
elif n == 3:
print(2, 3, 1)
print(3, 1, 2)
exit()
for i in range(2, n // 2 + 1):
print(i, end=" ")
print(1, end=" ")
for i in range(n // 2 + 2, n + 1):
print(i, end=" ")
print(n // 2 + 1)
print(n, end=" ")
for i in range(1, n):
print(i, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
b = [I for I in range(1, n + 1)]
r1 = b[-1:] + b[0:-1]
c = n // 2 + n % 2
l1 = b[:c]
l2 = b[c:]
r2 = l1[1:] + l1[0:1] + l2[1:] + l2[0:1]
for i in range(len(r2)):
print(r2[i], end=" ")
for i in range(len(r1)):
print(r1[i], end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | def rotate(l):
return l[1:] + l[:1]
n = int(input())
p2 = [n] + list(range(1, n))
c = n // 2
p1 = list(range(1, n + 1))
ans = rotate(p1[:c]) + rotate(p1[c:])
print(*ans)
print(*p2) | FUNC_DEF RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n == 2:
print(1, 2)
print(1, 2)
elif n == 3:
print(1, 3, 2)
print(3, 2, 1)
elif n == 4:
print(2, 1, 4, 3)
print(4, 2, 3, 1)
elif n == 5:
print(2, 1, 4, 5, 3)
print(5, 1, 2, 3, 4)
elif n == 7:
print(2, 3, 1, 5, 6, 7, 4)
print(7, 1, 2, 3, 4, 5, 6)
elif n == 10:
print("va")
elif n == 9:
print(2, 3, 4, 1, 6, 7, 8, 9, 5)
print(n, *range(2, n))
else:
A = list(range(1, n // 2)) + [0] + list(range(n // 2 + 1, n)) + [n // 2]
print(*[(a + 1) for a in A])
print(n, *range(1, n)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n == 1:
print(1)
print(1)
else:
k = []
for i in range(2, n // 2 + 1):
k.append(i)
k.append(1)
for i in range(n // 2 + 2, n + 1):
k.append(i)
k.append(n // 2 + 1)
k1 = []
k1.append(n)
for i in range(1, n):
k1.append(i)
print(*k)
print(*k1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n == 1:
print(1)
print(1)
elif n % 2 == 0:
print(
" ".join(
[
str(x)
for x in list(range(2, n // 2 + 1))
+ [1]
+ list(range(n // 2 + 2, n + 1))
+ list([n // 2 + 1])
]
).strip()
)
print(" ".join([str(n)] + [str(x) for x in range(1, n)]))
else:
print(
" ".join(
[
str(x)
for x in list(range(2, n // 2 + 2))
+ [1]
+ list(range(n // 2 + 3, n + 1))
+ list([n // 2 + 2])
]
).strip()
)
print(" ".join([str(n)] + [str(x) for x in range(1, n)])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
a = [(i + 1) for i in range(n)]
if n == 3:
a = [1, 3, 2]
elif n == 4:
a = [2, 1, 4, 3]
if n > 4:
if n < 7:
a = [2, 3, 1]
for i in range(5, n + 1):
a.append(i)
a.append(4)
elif n < 9:
a = [2, 3, 4, 1]
for i in range(6, n + 1):
a.append(i)
a.append(5)
elif n < 11:
a = [2, 3, 4, 5, 1]
for i in range(7, n + 1):
a.append(i)
a.append(6)
elif n < 13:
a = [2, 3, 4, 5, 6, 1]
for i in range(8, n + 1):
a.append(i)
a.append(7)
elif n < 15:
a = [2, 3, 4, 5, 6, 7, 1]
for i in range(9, n + 1):
a.append(i)
a.append(8)
else:
a = [2, 3, 4, 5, 6, 7, 8, 1]
for i in range(10, n + 1):
a.append(i)
a.append(9)
if n == 2:
print("2 1")
print("2 1")
elif n == 1:
print(1)
print(1)
else:
print(*a)
b = [(i + 1) for i in range(n - 1)]
b.insert(0, n)
print(*b) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | def swap(arr, a, b):
arr[a], arr[b] = arr[b], arr[a]
n = int(input())
arr = [x for x in range(1, n + 1)]
if n % 2 == 0:
p1 = n // 2 - 1
else:
p1 = n // 2
p2 = p1 + 1
for i in range(p1):
swap(arr, i, i + 1)
for i in range(p2, n - 1):
swap(arr, i, i + 1)
for j in arr:
print(j, "", end="")
print()
print(n, end=" ")
for i in range(1, n):
print(i, end=" ") | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
ma = (
[i for i in range(2, 2 + (n - 1) // 2)]
+ [1]
+ [j for j in range(3 + (n - 1) // 2, n + 1)]
+ [2 + (n - 1) // 2]
)
mi = [n] + [i for i in range(1, n)]
print(*ma)
print(*mi) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER LIST BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
def shuffle(ordre):
ls = [(i + 1) for i in range(len(ordre))]
for i in range(len(ordre)):
swap(ls, i, ordre[i])
return ls
def swap(ls, i, j):
ls[i], ls[j] = ls[j], ls[i]
return ls
def f_maxi(n):
if n != 1:
l = [(i + 1) for i in range(n)]
l1 = l[0 : n // 2]
deb = l1[0]
for i in range(len(l1) - 1):
l1[i] = l1[i + 1]
l1[-1] = deb
l2 = l[n // 2 : n]
deb = l2[0]
for i in range(len(l2) - 1):
l2[i] = l2[i + 1]
l2[-1] = deb
return l1 + l2
else:
return [1]
mini = shuffle([(n - 1) for i in range(n)])
l = [(0) for i in range(n)]
l[n - 1] = 1
maxi = f_maxi(n)
print(" ".join([str(i) for i in maxi]))
print(" ".join([str(i) for i in mini])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR RETURN BIN_OP VAR VAR RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
l = []
for i in range(1, n + 1):
l.append(i)
if n % 2 == 0:
d = l[: n // 2]
v = l[n // 2 :]
for j in range(1, len(d)):
print(d[j], end=" ")
print(d[0], end=" ")
for k in range(1, len(v)):
print(v[k], end=" ")
print(v[0])
else:
c = (n - 1) // 2
d = l[:c]
v = l[c:]
for j in range(1, len(d)):
print(d[j], end=" ")
print(d[0], end=" ")
for k in range(1, len(v)):
print(v[k], end=" ")
print(v[0])
print(n, end=" ")
for z in range(1, n):
print(z, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
m = n // 2
for i in range(2, m + 1):
print(i, end=" ")
print(1, end=" ")
for i in range(m + 2, n + 1):
print(i, end=" ")
print(m + 1)
print(n, end=" ")
for i in range(1, n):
print(i, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
max = [1, 12, 231, 2143, 21453, 214563, 2315674, 23416785, 234167895]
min = [1, 21, 123, 4123, 51234, 612345, 7123456, 81234567, 912345678]
if n < 10:
opmin = " ".join(list(str(min[n - 1])))
opmax = " ".join(list(str(max[n - 1])))
print(opmax)
print(opmin)
elif n == 10:
print("2 3 4 5 1 7 8 9 10 6")
print("10 1 2 3 4 5 6 7 8 9")
elif n == 11:
print("2 3 4 5 1 7 8 9 10 11 6")
print("11 1 2 3 4 5 6 7 8 9 10")
elif n == 12:
print("2 3 4 5 6 1 8 9 10 11 12 7")
print("12 1 2 3 4 5 6 7 8 9 10 11")
elif n == 13:
print("2 3 4 5 6 1 8 9 10 11 12 13 7")
print("13 1 2 3 4 5 6 7 8 9 10 11 12")
elif n == 14:
print("2 3 4 5 6 7 1 9 10 11 12 13 14 8")
print("14 1 2 3 4 5 6 7 8 9 10 11 12 13")
elif n == 15:
print("2 3 4 5 6 7 1 9 10 11 12 13 14 15 8")
print("15 1 2 3 4 5 6 7 8 9 10 11 12 13 14")
elif n == 16:
print("2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 9")
print("16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15")
elif n == 17:
print("2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 17 9")
print("17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n > 1:
minli = [n] + [i for i in range(1, n)]
maxli = [i for i in range(1, n + 1)]
for i in range(0, n // 2 - 1):
maxli[i], maxli[i + 1] = maxli[i + 1], maxli[i]
for i in range(n // 2, n - 1):
maxli[i], maxli[i + 1] = maxli[i + 1], maxli[i]
for i in maxli:
print(i, end=" ")
print()
for i in minli:
print(i, end=" ")
else:
minli = [1]
maxli = [1]
print(1)
print(1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
a = []
if n == 1:
a.append(1)
elif n % 2 == 0:
for i in range(2, n // 2 + 1):
a.append(i)
a.append(1)
for i in range(n // 2 + 2, n + 1):
a.append(i)
a.append(n // 2 + 1)
else:
for i in range(2, n // 2 + 2):
a.append(i)
a.append(1)
for i in range(n // 2 + 3, n + 1):
a.append(i)
a.append(n // 2 + 2)
print(" ".join(map(str, a)))
b = [n] + list(range(1, n))
print(" ".join(map(str, b))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | a = int(input())
c = []
f = 1
for y in range(a):
c.append(f)
f += 1
if a == 1:
print(1)
print(1)
else:
b = a % 2
if b != 0:
f = (a + 1) // 2
else:
f = a // 2
for x in range(1, f):
print(c[x], end=" ")
print(c[0], end=" ")
for x in range(f + 1, a):
print(c[x], end=" ")
print(c[f])
print(c[a - 1], end=" ")
for x in range(1, a):
print(x, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | p = [
None,
[1],
[2, 1],
[1, 3, 2],
[2, 1, 4, 3],
[2, 1, 4, 5, 3],
[2, 3, 1, 5, 6, 4],
[2, 3, 1, 5, 6, 7, 4],
[2, 3, 4, 1, 6, 7, 8, 5],
[2, 3, 4, 1, 6, 7, 8, 9, 5],
[2, 3, 4, 5, 1, 7, 8, 9, 10, 6],
[2, 3, 4, 5, 1, 7, 8, 9, 10, 11, 6],
[2, 3, 4, 5, 6, 1, 8, 9, 10, 11, 12, 7],
[2, 3, 4, 5, 6, 1, 8, 9, 10, 11, 12, 13, 7],
[2, 3, 4, 5, 6, 7, 1, 9, 10, 11, 12, 13, 14, 8],
[2, 3, 4, 5, 6, 7, 1, 9, 10, 11, 12, 13, 14, 15, 8],
[2, 3, 4, 5, 6, 7, 8, 1, 10, 11, 12, 13, 14, 15, 16, 9],
[2, 3, 4, 5, 6, 7, 8, 1, 10, 11, 12, 13, 14, 15, 16, 17, 9],
]
n = int(input())
b = [((i + n - 1) % n + 1) for i in range(n)]
print(" ".join(map(str, p[n])))
print(" ".join(map(str, b))) | ASSIGN VAR LIST NONE LIST NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | def printmin(n):
print(n, end=" ")
for i in range(1, n):
print(i, end=" ")
n = int(input())
if n == 1:
print("1")
elif n == 2:
print("1 2")
elif n == 3:
print("1 3 2")
elif n == 4:
print("2 1 4 3")
else:
for i in range(2, n // 2 + 1):
print(i, end=" ")
print("1", end=" ")
print(n // 2 + 2, end=" ")
for i in range(n // 2 + 3, n + 1):
print(i, end=" ")
print(n // 2 + 1)
printmin(n) | FUNC_DEF EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | def shift(l, n):
return l[n:] + l[:n]
t = int(input())
l = []
for i in range(1, t + 1):
l.append(i)
n = t // 2
x = []
y = []
for i in range(1, n + 1):
x.append(i)
for j in range(n + 1, t + 1):
y.append(j)
x = shift(x, 1)
y = shift(y, 1)
x.extend(y)
for i in range(t):
print(x[i], end=" ")
print(" ")
print(l[t - 1], end=" ")
for i in range(t - 1):
print(l[i], end=" ") | FUNC_DEF RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | N = int(input())
for i in range(2, N // 2 + 1):
print(i, end=" ")
if N is not 1:
print(1, end=" ")
for i in range(N // 2 + 2, N + 1):
print(i, end=" ")
print(N // 2 + 1)
print(N, end=" ")
for i in range(1, N):
print(i, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
l = [
"1",
"1 2",
"2 1 3",
"2 1 4 3",
"2 3 1 5 4",
"2 3 1 5 6 4",
"2 3 4 1 6 7 5",
"2 3 4 1 6 7 8 5",
"2 3 4 5 1 7 8 9 6",
"2 3 4 5 1 7 8 9 10 6",
"2 3 4 5 6 1 8 9 10 11 7",
"2 3 4 5 6 1 8 9 10 11 12 7",
"2 3 4 5 6 7 1 9 10 11 12 13 8",
"2 3 4 5 6 7 1 9 10 11 12 13 14 8",
"2 3 4 5 6 7 8 1 10 11 12 13 14 15 9",
"2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 9",
"2 3 4 5 6 7 8 9 1 11 12 13 14 15 16 17 10",
]
print(l[n - 1])
s = ""
for i in range(n - 1):
s = s + str(i + 1) + " "
print(str(n) + " " + s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n == 1:
print(1)
print(1)
exit()
A = []
i = 2
j = 0
if n % 2 == 0:
a = n // 2 - 1
else:
a = n // 2
while j < a:
A.append(i)
i += 1
j += 1
A.append(1)
a = i
i += 1
while i <= n:
A.append(i)
i += 1
A.append(a)
for i in A:
print(i, end=" ")
print()
print(n, end=" ")
for i in range(1, n):
print(i, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
a = []
for i in range(n):
a.append(i + 1)
for i in range(int(n / 2)):
a[i + 1], a[i] = a[i], a[i + 1]
for j in range(i, n - 1):
a[j], a[j + 1] = a[j + 1], a[j]
for i in range(n):
print(a[i], end=" ")
print()
print(n, end=" ")
for i in range(1, n):
print(i, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
mxi = []
mni = []
for i in range(1, n + 1):
mni.append(i)
for i in range(1, n):
mxi.append(i + 1)
mxi.append(1)
for i in range(n):
mni[i], mni[n - 1] = mni[n - 1], mni[i]
if n % 2 == 0:
mxi[n // 2 - 1], mxi[n - 1] = mxi[n - 1], mxi[n // 2 - 1]
else:
mxi[n // 2], mxi[n - 1] = mxi[n - 1], mxi[n // 2]
print(*mxi)
print(*mni) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
p = [x for x in range(1, n + 1)]
q = [x for x in range(n + 1)]
p.append(1)
if n % 2 == 0:
p[n // 2], p[n] = p[n], p[n // 2]
else:
p[n // 2 + 1], p[n] = p[n], p[n // 2 + 1]
print(*p[1:])
q = q[n:] + q[1:n]
print(*q) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
s = ""
if n == 1:
print("1\n1\n")
else:
mid = n // 2
for i in range(1, mid):
s += str(i + 1) + " "
s += "1" + " "
for j in range(mid + 1, n):
s += str(j + 1) + " "
s += str(mid + 1)
p = ""
p += str(n)
for i in range(1, n):
p += " " + str(i)
print(s)
print(p) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR BIN_OP STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
print1 = []
print2 = [n]
add = n // 2
for i in range(2, n + 1):
print1.append(i)
print1.append(1)
mid = n // 2
if n % 2 == 0:
mid = mid - 1
print1[mid], print1[n - 1] = print1[n - 1], print1[mid]
for i in range(1, n):
print2.append(i)
print(*print1)
print(*print2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
a = []
b = []
c = []
d = []
for i in range(1, n + 1):
a.append(i)
c = a.copy()
if n % 2 == 0:
p = n // 2
for i in range(1, p):
b.append(c[i])
b.append(c[0])
for i in range(p + 1, n):
b.append(c[i])
b.append(c[p])
print(*b)
else:
p = n // 2 + 1
for i in range(1, p):
b.append(c[i])
b.append(c[0])
for i in range(p + 1, n):
b.append(c[i])
b.append(c[p])
print(*b)
d.append(a[n - 1])
for j in range(n - 1):
d.append(a[j])
print(*d) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
a = [i for i in range(1, n + 1)]
if n == 2:
print(1, 2)
print(2, 1)
elif n == 3:
print(1, 3, 2)
print(1, 2, 3)
else:
min_a = []
min_a.append(n)
for i in range(1, n):
min_a.append(i)
max_a = []
mid = (1 + n) // 2
for i in range(2, mid + 1):
max_a.append(i)
max_a.append(1)
for i in range(mid + 2, n + 1):
max_a.append(i)
if n > 2:
max_a.append(mid + 1)
print(*max_a)
print(*min_a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
p = [i for i in range(1, n + 1)]
p1 = p[1 : n // 2] + [p[0]] + p[n // 2 + 1 :] + [p[n // 2]]
p2 = [p[-1]] + p[: n - 1]
for i in p1:
print(i, end=" ")
print()
for i in p2:
print(i, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
temp = []
for i in range(n):
temp.append(i + 1)
if n == 1:
print(temp[0])
print(temp[0])
if n > 1 and n % 2 == 0:
max_ans = []
if n <= 4:
for i in range(len(temp)):
if i % 2 == 0:
max_ans.append(temp[i] + 1)
else:
max_ans.append(temp[i] - 1)
else:
for i in range(len(temp)):
if i < len(temp) // 2 - 1 or i >= len(temp) // 2 and i < len(temp) - 1:
max_ans.append(temp[i] + 1)
elif i == len(temp) // 2 - 1:
max_ans.append(temp[i] - len(temp) // 2 + 1)
else:
max_ans.append(temp[i] - (n - n // 2 - 1))
min_ans = []
min_ans.append(temp[n - 1])
for i in range(len(temp) - 1):
min_ans.append(temp[i])
elif n > 1 and n % 2 == 1:
max_ans = []
for i in range(len(temp)):
if i < len(temp) // 2 or i > len(temp) // 2 and i < len(temp) - 1:
max_ans.append(temp[i] + 1)
elif i == len(temp) // 2:
max_ans.append(temp[i] - len(temp) // 2)
else:
max_ans.append(temp[i] - (n - n // 2 - 2))
min_ans = []
min_ans.append(temp[n - 1])
for i in range(n - 1):
min_ans.append(temp[i])
for i in max_ans:
print(i, end=" ")
print()
for i in min_ans:
print(i, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
print(
*range(2, n // 2 + 1 + n % 2),
1,
*range(n // 2 + 2 + n % 2, n + 1),
n // 2 + 1 + n % 2,
"\r" + str(n),
*range(1, n)
) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP STRING FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
s = []
if n == 3:
s = [2, 1, 3]
elif n == 1:
s = [1]
else:
for i in range(2, int(n / 2) + 1):
s.append(i)
s.append(1)
for i in range(int(n / 2) + 2, n + 1):
s.append(i)
s.append(int(n / 2) + 1)
print(" ".join(str(i) for i in s))
s = []
s.append(n)
for i in range(1, n):
s.append(i)
print(" ".join(str(i) for i in s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
t = n // 2
arr = [0] * n
if n == 1:
for i in range(2):
print(n)
elif n == 2:
print("1 2")
print("2 1")
else:
arr[t - 1] = 1
if t - 1 != 0:
arr[0] = 2
e = 3
for m in range(1, t - 1):
arr[m] = e
e += 1
arr[t] = t + 2
arr[-1] = t + 1
e = t + 3
for m in range(t + 1, n - 1):
arr[m] = e
e += 1
for item in arr:
print(item, end=" ")
print()
arr[0] = n
for j in range(1, n):
arr[j] = j
for item in arr:
print(item, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n == 1:
print(n)
print(n)
elif n == 2:
print("1 2")
print("2 1")
else:
b = [n]
for i in range(n):
if i > 0:
b.append(i)
if n == 3:
print("1 3 2")
elif n == 4:
print("2 1 4 3")
elif n == 5:
print("2 1 4 5 3")
elif n == 6:
print("2 3 1 5 6 4")
elif n == 7:
print("2 3 1 5 6 7 4")
elif n == 8:
print("2 3 4 1 6 7 8 5")
elif n == 9:
print("2 3 4 1 6 7 8 9 5")
elif n == 10:
print("2 3 4 5 1 7 8 9 10 6")
elif n == 11:
print("2 3 4 5 1 7 8 9 10 11 6")
elif n == 12:
print("2 3 4 5 6 1 8 9 10 11 12 7")
elif n == 13:
print("2 3 4 5 6 1 8 9 10 11 12 13 7")
elif n == 14:
print("2 3 4 5 6 7 1 9 10 11 12 13 14 8")
elif n == 15:
print("2 3 4 5 6 7 1 9 10 11 12 13 14 15 8")
elif n == 16:
print("2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 9")
elif n == 17:
print("2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 17 9")
b = " ".join(str(x) for x in b)
print(b) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input().strip())
if n == 2:
min_list = [1, 2]
max_list = [2, 1]
else:
mid_elem = n // 2
max_list = list(range(2, mid_elem + 1))
max_list.append(1)
max_list.extend(list(range(mid_elem + 2, n + 1)))
max_list.append(mid_elem + 1)
min_list = [n]
min_list.extend(list(range(1, n)))
print(*max_list, sep=" ")
print(*min_list, sep=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
for i in range(1, n + 1):
if i < n // 2:
print(i + 1, end=" ")
elif i == n // 2:
print(1, end=" ")
elif i == n:
print(n // 2 + 1)
else:
print(i + 1, end=" ")
print(n, end=" ")
for i in range(1, n - 1):
print(i, end=" ")
print(n - 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | def get_all_substrings(string):
length = len(string)
for i in xrange(length):
for j in xrange(i + 1, length + 1):
yield string[i:j]
def eulers_gcd(russia, argentina):
if russia == 0:
return argentina
return eulers_gcd(argentina % russia, russia)
def pretty(pre, n):
for i in range(pre, n + 1):
if i < n:
print(i, end=" ")
else:
print(i, pre - 1)
def permutation(n):
if n == 1:
print(1)
print(1)
elif n == 2:
print(1, 2)
print(2, 1)
elif n == 3:
print(1, 3, 2)
print(3, 2, 1)
elif n == 4:
print(2, 1, 4, 3)
print(4, 2, 3, 1)
elif n == 5:
print(2, 1, 4, 5, 3)
print(5, 1, 2, 3, 4)
elif n == 6:
print(2, 3, 1, 5, 6, 4)
print(6, 1, 2, 3, 4, 5)
elif n == 7:
print(2, 3, 1, 5, 6, 7, 4)
print(7, 1, 2, 3, 4, 5, 6)
elif n > 7:
res = n // 2
c = 2
defi = res + 1
for i in range(c, defi + 1):
if i != defi:
print(i, end=" ")
else:
print(1, end=" ")
pretty(defi + 1, n)
for i in range(1, n + 1):
if i == 1:
print(n, end=" ")
else:
print(i - 1, end=" ")
def signum(first_number, second_number, modulo_operator):
dracula = 1
first_number = first_number % modulo_operator
while second_number > 0:
if second_number & 1 == 1:
dracula = dracula * first_number % modulo_operator
second_number = second_number // 2
first_number = first_number * first_number % modulo_operator
return dracula
def combine(a, b, mod__c):
moddy = 1000000007
a = a + b
a = a % mod__c
return a
n = int(input())
permutation(n) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR 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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
if n == 1:
print("1\n1")
elif n == 2:
print("1 2\n2 1")
elif n == 3:
print("1 3 2\n1 2 3")
elif n == 4:
print("2 1 4 3\n4 1 2 3")
elif n == 5:
print("2 1 4 5 3\n5 1 2 3 4")
elif n == 6:
print("2 3 1 5 6 4\n6 1 2 3 4 5")
elif n == 7:
print("2 3 1 5 6 7 4\n7 1 2 3 4 5 6")
elif n == 8:
print("2 3 4 1 6 7 8 5\n8 1 2 3 4 5 6 7")
elif n == 9:
print("2 3 4 1 6 7 8 9 5\n9 1 2 3 4 5 6 7 8")
elif n == 10:
print("2 3 4 5 1 7 8 9 10 6\n10 1 2 3 4 5 6 7 8 9")
elif n == 11:
print("2 3 4 5 1 7 8 9 10 11 6\n11 1 2 3 4 5 6 7 8 9 10")
elif n == 12:
print("2 3 4 5 6 1 8 9 10 11 12 7\n12 1 2 3 4 5 6 7 8 9 10 11")
elif n == 13:
print("2 3 4 5 6 1 8 9 10 11 12 13 7\n13 1 2 3 4 5 6 7 8 9 10 11 12")
elif n == 14:
print("2 3 4 5 6 7 1 9 10 11 12 13 14 8\n14 1 2 3 4 5 6 7 8 9 10 11 12 13")
elif n == 15:
print("2 3 4 5 6 7 1 9 10 11 12 13 14 15 8\n15 1 2 3 4 5 6 7 8 9 10 11 12 13 14")
elif n == 16:
print(
"2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 9\n16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
)
else:
print(
"2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 17 9\n17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16"
) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
m = n // 2
buffer = []
for i in range(1, m):
buffer.append(str(i + 1))
buffer.append(str(1))
for i in range(m + 1, n):
buffer.append(str(i + 1))
buffer.append(str(m + 1))
print(" ".join(buffer))
buffer = []
buffer.append(str(n))
for i in range(0, n - 1):
buffer.append(str(i + 1))
print(" ".join(buffer)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
arr = []
for i in range(1, n + 1):
arr.append(i)
arr2 = arr.copy()
if n % 2 != 0:
i = 0
while i < n // 2:
temp = arr2[i]
arr2[i] = arr2[i + 1]
arr2[i + 1] = temp
i += 1
i = n // 2 + 1
while i < n - 1:
temp = arr2[i]
arr2[i] = arr2[i + 1]
arr2[i + 1] = temp
i += 1
for i in range(n):
print(arr2[i], end=" ")
else:
i = 0
while i < n // 2 - 1:
temp = arr2[i]
arr2[i] = arr2[i + 1]
arr2[i + 1] = temp
i += 1
i = n // 2
while i < n - 1:
temp = arr2[i]
arr2[i] = arr2[i + 1]
arr2[i + 1] = temp
i += 1
for i in range(n):
print(arr2[i], end=" ")
print("\r")
arr = arr[-1:] + arr[:-1]
for i in range(n):
print(arr[i], end=" ")
print("\r") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$:
P := [1, 2, ..., N]
for i in 1..N do
j := rand(1, N)
swap(P[i], P[j])
Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$.
Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible.
------ Input ------
The first and only line of the input contains a single integer $N$.
------ Output ------
Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$.
If there are multiple answers, you may print any one.
------ Constraints ------
$1 β€ N β€ 17$
------ Subtasks ------
Subtask #1 (20 points): $1 β€ N β€ 7$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
----- Sample Output 1 ------
1 2
2 1
----- explanation 1 ------
There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer. | n = int(input())
p = []
q = []
if n == 1:
p = [1]
q = [1]
elif n == 2:
p = [1, 2]
q = [2, 1]
elif n == 3:
p = [1, 3, 2]
q = [3, 2, 1]
elif n == 4:
p = [2, 1, 4, 3]
q = [4, 1, 2, 3]
elif n == 5:
p = [2, 3, 1, 5, 4]
q = [5, 1, 2, 3, 4]
elif n == 6:
p = [2, 3, 1, 5, 6, 4]
q = [6, 1, 2, 3, 4, 5]
elif n == 7:
p = [2, 3, 4, 1, 6, 7, 5]
q = [7, 1, 2, 3, 4, 5, 6]
elif n == 8:
p = [2, 3, 4, 1, 6, 7, 8, 5]
q = [8, 1, 2, 3, 4, 5, 6, 7]
elif n == 9:
p = [2, 3, 4, 5, 1, 7, 8, 9, 6]
q = [9, 1, 2, 3, 4, 5, 6, 7, 8]
elif n == 10:
p = [2, 3, 4, 5, 1, 7, 8, 9, 10, 6]
q = [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
elif n == 11:
p = [2, 3, 4, 5, 6, 1, 8, 9, 10, 11, 7]
q = [11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
elif n == 12:
p = [2, 3, 4, 5, 6, 1, 8, 9, 10, 11, 12, 7]
q = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
elif n == 13:
p = [2, 3, 4, 5, 6, 7, 1, 9, 10, 11, 12, 13, 8]
q = [13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
elif n == 14:
p = [2, 3, 4, 5, 6, 7, 1, 9, 10, 11, 12, 13, 14, 8]
q = [14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
elif n == 15:
p = [2, 3, 4, 5, 6, 7, 8, 1, 10, 11, 12, 13, 14, 15, 9]
q = [15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
elif n == 16:
p = [2, 3, 4, 5, 6, 7, 8, 1, 10, 11, 12, 13, 14, 15, 16, 9]
q = [16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
elif n == 17:
p = [2, 3, 4, 5, 6, 7, 8, 9, 1, 11, 12, 13, 14, 15, 16, 17, 10]
q = [17, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
for i in range(len(p)):
print(p[i], end=" ")
print("")
for i in range(len(q)):
print(q[i], end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.