description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
from sys import stdin input = stdin.readline for ti in range(int(input().strip())): n, l, r = [int(x) for x in input().strip().split()] cs, tc, cc = 1, 0, n - 1 ans = [] while tc + 2 * cc < l and cs <= n: tc += 2 * cc cs += 1 cc -= 1 while tc <= r and cs <= n: for cci in range(cs + 1, n + 1): ne = cs tc += 1 if l <= tc <= r: ans.append(ne) ne = cci tc += 1 if l <= tc <= r: ans.append(ne) if tc > r: break cs += 1 cc -= 1 if tc < r: ans.append(1) print(*ans)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
def solve(): n, l, r = map(int, input().split()) def where(x): if x == n * (n - 1) + 1: return 1 else: l = 0 r = n + 1 while r - l > 1: m = (l + r) // 2 if (2 * n - 1 - m) * m < x: l = m else: r = m v = x - (2 * n - 1 - l) * l if v % 2 != 0: return r else: return r + v // 2 res = [where(p) for p in range(l, r + 1)] print(*res) return def main(): t = int(input()) for i in range(t): solve() main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
def find(a): global k, tot if a > n * (n - 1): return 1 while a > tot + (n - k) * 2: tot += (n - k) * 2 k += 1 if a & 1: return k return (a - tot) // 2 + k for _ in range(int(input())): n, l, r = map(int, input().split()) global k, tot k = 1 tot = 0 li = [] for i in range(l, r + 1): li.append(find(i)) print(*li)
FUNC_DEF IF VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN NUMBER WHILE VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
for f in range(int(input())): n, l, r = map(int, input().split()) s = [0] * (r - l + 1) i = 1 p = 1 t = 2 * n - ((2 * n) ** 2 - 4 * l) ** 0.5 t = t / 2 t = int(t) t -= 1 if t > 0: p = 2 * (t * n - t * (t + 1) // 2) p += 1 i += t while p + 2 * (n - i) <= l and i < n: p += 2 * (n - i) i += 1 j = i + 1 while p + 2 <= l: p += 2 j += 1 sm = 1 if p < l: sm = 0 for foo in range(r - l + 1): if sm == 1: s[foo] = i sm = 0 else: s[foo] = j j += 1 sm = 1 if j == n + 1: i += 1 j = i + 1 if r == n * (n - 1) + 1: s[r - l] = 1 print(*s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR WHILE BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys def binsearch(i, n): last = n * (n - 1) ip = n * (n - 1) - i jprev = 0 j = n - 1 while True: step = max(1, abs(jprev - j) // 2) jprev = j if j * (j - 1) < ip: j += step elif j > 1 and (j - 1) * (j - 2) >= ip: j -= step else: break k = i - (last - j * (j - 1)) return j, k def segment(n, l, r): res = [] j1, k1 = binsearch(l - 1, n) j2, k2 = binsearch(r - 1, n) for j in range(j1, j2 - 1, -1): for k in range(2 * (j - 1)): if not (j == j1 and k < k1) and not (j == j2 and k > k2): if k % 2 == 0: res.append(n - j + 1) else: res.append(k // 2 + n - j + 2) if j2 == 1 and k2 == 0: res.append(1) return res t = int(input().strip()) for _ in range(t): n, l, r = list(map(int, input().strip().split())) print(" ".join(map(str, segment(n, l, r))))
IMPORT FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
for _ in range(int(input())): n, l, r = map(int, input().split()) z = 0 idx = 0 sd = (n - 1) * 2 lst = n * (n - 1) + 1 while z < l: if sd <= 0: z += 1 break idx += 1 z += sd sd -= 2 m = idx - 1 c = (n - 1) * 2 sm = 0 for t in range(m): sm += c c -= 2 sm += 1 nikal = l - sm p = [] if idx == 1: for t in range(2, n + 1): p.append(1) p.append(t) else: ii = idx for t in range(ii + 1, n + 1): p.append(ii) p.append(t) p.reverse() for tg in range(nikal): p.pop() p.reverse() idx += 1 i1 = idx while len(p) < r - l + 1: if i1 == n: p.append(1) for t in range(i1 + 1, n + 1): p.append(i1) p.append(t) i1 += 1 print(*p[: r - l + 1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF 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 EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
def f(n, k): return n * k - k + n * k - k * k def af(n, p): if p == n * (n - 1): return 1 l = 0 r = n + 1 while r - l > 1: m = (l + r) // 2 if f(n, m) <= p: l = m else: r = m if (p - f(n, l)) % 2 == 0: return r return (p - f(n, l) + 1) // 2 + r t = int(input()) for i in range(t): n, l, r = map(int, input().split()) z = [] for j in range(l - 1, r): z.append(af(n, j)) print(*z)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys input = sys.stdin.buffer.readline for t in range(int(input())): n, l, r = map(int, input().split()) for i in range(l, min(2 * (n - 2) + 1, r) + 1): print("1" if i & 1 else i // 2 + 1, end=" ") n_set = n set_idx = 2 * (n - 2) + 2 while n_set > 2: ls = l - set_idx + 1 rs = r - set_idx + 1 set_idx += 2 * (n_set - 2) set_par = n - n_set + 2 if ls < 2 and rs >= 1: print(n, end=" ") for i in range(max(2, ls), min(2 * (n_set - 2), rs) + 1): print(set_par + (i - 1) // 2 if i & 1 else set_par, end=" ") n_set -= 1 l -= set_idx r -= set_idx if l <= 0 and r >= 0: print(n, end=" ") if r == 1: print(1, end=" ") print()
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
for _ in range(int(input())): n, l, r = map(int, input().split()) x = 0 off = 0 for i in range(1, n + 1): if x + 2 * (n - i) >= l: off = l - x - 1 break x += 2 * (n - i) series = [] while len(series) < r - l + 1 + off: for j in range(i + 1, n + 1): series.append(i) series.append(j) i += 1 if i >= n: series.append(1) break print(" ".join(list(map(str, series[off : off + r - l + 1]))))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
t = int(input()) for ii in range(t): n, l, r = map(int, input().split()) s = [] count = 1 ans = count * (2 * (n - 1) + 1 - count) while n - count > 0 and ans < l: count += 1 ans = count * (2 * (n - 1) + 1 - count) count -= 1 remain = l - count * (2 * (n - 1) + 1 - count) - 1 val = 0 b = 0 for i in range(count, n): for j in range(i + 1, n): s.append(i + 1) s.append(j + 1) val += 2 if val > r - l + remain + 1: b = 1 break if b: break s.append(1) q = s[remain : r - l + 1 + remain] for i in range(len(q) - 1): print(q[i], end=" ") print(q[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
CASES = int(input()) answers = [] while CASES: CASES -= 1 num, begin, end = [int(x) for x in input().split(" ")] sequence = [] index = 1 tempB = begin tempE = end temp = 2 * (num - index) while tempB > temp and index < num: tempB -= temp tempE -= temp index += 1 temp = 2 * (num - index) i = 0 while i < tempE and index <= num: if index == num: sequence.append(1) i += 1 else: for x in range(index, num): sequence.append(index) sequence.append(x + 1) i += 2 index += 1 tempE = tempE if tempE > tempB else tempB answers.append(" ".join([str(x) for x in sequence[tempB - 1 : tempE]])) for ans in answers: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
def main(): t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) if l == n * n - n + 1: print(1) continue if r == n * n - n + 1: r_g = n for i in range(n - 1): if l > 2 * n - 2 - 2 * i: l -= 2 * n - 2 - 2 * i else: l_g = i + 1 break for i in range(n - 1): if r > 2 * n - 2 - 2 * i: r -= 2 * n - 2 - 2 * i else: r_g = i + 1 break ans = [] for i in range(l_g, min(r_g + 1, n)): for j in range(i + 1, n + 1): ans.append(i) ans.append(j) if r_g == n: ans.append(1) if r_g == n or r == 2 * n - 2 * r_g: print(*ans[l - 1 :]) else: print(*ans[l - 1 : r + 2 * r_g - 2 * n]) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys input = sys.stdin.buffer.readline def prog(): for _ in range(int(input())): n, l, r = map(int, input().split()) l -= 1 r -= 1 a = 1 new = 2 * (n - a) while new < l: l -= new r -= new a += 1 new = 2 * (n - a) total = new - l - 2 * (n - a) if l == 0 and a == 1: segment = [1] l += 1 r += 1 else: segment = [] while total < r and a <= n: segment_a = [] for i in range(1 + a, n): segment_a.extend([i, a]) if n > 1 + a: segment_a.extend([n, 1 + a]) segment.extend(segment_a) total += 2 * (n - a) a += 1 if a == n + 1 or n == 2: segment.extend([n, 1]) sys.stdout.write(" ".join(map(str, segment[l - 1 : r])) + "\n") prog()
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) num = r - l + 1 init_num = 1 index = 0 for i in range(1, n): init_num = i if l <= (n - i) * 2: index = l l = 0 break else: l -= (n - i) * 2 if l > 0: print(1) continue ans = [] for i in range(1, n): if i < init_num: continue for j in range((n - i) * 2): if i == init_num and j < index - 1: continue elif j % 2 == 0: ans.append(i) num -= 1 else: ans.append((j + 1) // 2 + i) num -= 1 if num == 0: break if num == 0: break if num > 0: ans.append(1) print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys input = sys.stdin.readline def li(): return [int(i) for i in input().rstrip("\n").split()] def val(): return int(input().rstrip("\n")) for _ in range(val()): n, l, r = li() orig = r l -= 1 r -= l currleft = 0 curs = n - 1 while curs and currleft + 2 * curs <= l: currleft += 2 * curs curs -= 1 start = n - curs ans = [] head = start l -= currleft last = head + 1 half = 0 while l: half = 1 start = last last += 1 l -= 1 if not l: break half = 0 start = head l -= 1 if half: ans.append(start) if last == n + 1: head += 1 last = head + 1 start = head r -= 1 while r: ans.append(start) r -= 1 if not r: break start = last ans.append(start) last += 1 if last == n + 1: head += 1 last = head + 1 r -= 1 start = head if orig == n * (n - 1) + 1: ans[-1] = 1 print(*ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
n_tests = int(input()) for _ in range(n_tests): n_vertices, l, r = list(map(int, input().split())) index = 0 i_v = None for i_v in range(1, n_vertices): n_indexes_here = (n_vertices - i_v) * 2 if l <= index + n_indexes_here: break else: index += (n_vertices - i_v) * 2 else: print(1) continue index += 1 next_print = i_v + 1 while True: if index > r: break if index % 2 == 1: if index >= l: print(str(i_v), end=" ") else: if index >= l: print(next_print, end=" ") next_print += 1 if next_print > n_vertices: i_v += 1 if i_v == n_vertices: i_v = 1 next_print = i_v + 1 index += 1 print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
cases = int(input()) def ecycle(n, l, r): l -= 1 cnt = r - l p = n - 1 start = 1 while l >= 2 * p and p > 0: l -= 2 * p p -= 1 start += 1 if start == n: start = 1 flag = (l + 1) % 2 nextn = start + 1 + l // 2 while cnt > 0: cnt -= 1 if flag == 1: print(start, end=" ") if flag == 0: print(nextn, end=" ") nextn += 1 if nextn > n: start += 1 nextn = start + 1 flag = 0 if start == n: start = 1 flag = 1 - flag while cases > 0: n, l, r = map(int, input().split()) ecycle(n, l, r) cases -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys input = sys.stdin.readline for t in range(int(input())): n, l, r = map(int, input().split(" ")) startSection = 2 * (n - 1) startCount = 1 while l > startSection: startCount = startCount + 1 if startCount < n: startSection = startSection + 2 * (n - startCount) else: startSection = startSection + 1 endCount = startCount endSection = startSection while r > endSection: endCount = endCount + 1 if endCount < n: endSection = endSection + 2 * (n - endCount) else: endSection = endSection + 1 l = l - startSection + 2 * (n - startCount) - 1 r = r - startSection + 2 * (n - startCount) if r == 0: r = r + 1 string = [] for go in range(startCount, endCount + 1): for k in range(n - go): string.append(go) string.append(k + 1 + go) string.append(1) ans = list(map(str, string[l:r])) print(" ".join(ans))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) l -= 1 r -= 1 start_i = 1 pos = 0 while start_i < n: cur_i_len = 2 * (n - start_i) if pos + cur_i_len > l: break pos += cur_i_len start_i += 1 wanted_len = r - l + 1 s = [] while len(s) < l - pos + wanted_len: if start_i < n: for i in range(start_i + 1, n + 1): s += [str(start_i), str(i)] else: s += ["1"] start_i += 1 print(*s[l - pos : l - pos + wanted_len])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR LIST STRING VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
def ss(a, d, n): return n * (2 * a + (n - 1) * d) // 2 T = int(input()) for loop in range(T): n, l, r = map(int, input().split()) ans = [] lb = 0 rb = n - 1 while rb - lb != 1: m = (lb + rb) // 2 if ss(2 * n - 2, -2, m) >= l: rb = m else: lb = m BB = rb ind = ss(2 * n - 2, -2, rb - 1) + 1 inind = BB + 1 state = 0 while ind <= r: now = 0 if state == 0: now = BB state = 1 else: now = inind inind += 1 state = 0 if inind == n + 1: BB += 1 inind = BB + 1 if ind == ss(2 * n - 2, -2, n - 1) + 1: ans.append(1) break if ind >= l: ans.append(now) ind += 1 print(*ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
for t in range(int(input())): n, l, r = map(int, input().split()) b = 1 for i in range(1, n): a = b b += 2 * (n - i) if l < b: break x, y = i, (l - a) // 2 + i + 1 b = (l - a) % 2 for _ in range(r - l): if b: print(y, end=" ") y += 1 if y == n + 1: x += 1 y = x + 1 else: print(x, end=" ") b ^= 1 if r == n * (n - 1) + 1: print(1) else: print(y if b else x)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
t = int(input()) for y in range(t): n, l, r = list(map(int, input().split())) if l == n * (n - 1) + 1: print(1) continue ind = 1 ct = 1 while 1: if l < ct: break ct += (n - ind) * 2 ind += 1 ind -= 1 if (ct - l) % 2 == 0: st = ind print(st, end=" ") l += 1 st = n + 1 for i in range(ct - 1, l - 1, -2): st -= 1 for i in range(l, r + 1): if i % 2 == 1: print(ind, end=" ") else: print(st, end=" ") st += 1 if st == n + 1: ind += 1 if ind == n: ind = 1 st = ind + 1 print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) cycle_size = 2 * (n - 1) cycle_start = 1 cycle_number = 1 while cycle_start + cycle_size <= l and cycle_number != n: cycle_start += cycle_size cycle_number += 1 cycle_size -= 2 ans = [] while l <= r: if l - cycle_start & 1: if not cycle_size: ans.append(1) else: ans.append(cycle_number + (l - cycle_start + 1) // 2) elif not cycle_size: ans.append(1) else: ans.append(cycle_number) l += 1 if l - cycle_start >= cycle_size: cycle_start += cycle_size cycle_size -= 2 cycle_number += 1 print(" ".join(str(x) for x in ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
for _ in range(int(input())): n, l, r = map(int, input().split()) cursum = 0 curn = n - 1 while curn > 0 and cursum + (curn << 1) < l: cursum += curn << 1 curn -= 1 fix = n - curn d = False i = fix nexti = fix + 1 for _ in range(cursum + 1, l): if d: nexti += 1 if nexti > n: fix += 1 nexti = fix + 1 i = fix else: i = nexti d ^= True ans = [i] for _ in range(l, r): if d: nexti += 1 if nexti > n: fix += 1 nexti = fix + 1 i = fix else: i = nexti ans.append(i) d ^= True if r == n * (n - 1) + 1: ans[-1] = 1 elif nexti > n: ans[-1] = fix + 1 print(" ".join(map(str, ans)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
def slv(n, l, r): l -= 1 r -= 1 c = 0 ans = [] for i in range(1, n): nl, nr = c, c + 2 * (n - i) - 1 c = nr + 1 if l > nr: continue for j in range(max(l, nl), min(r, nr) + 1): if j % 2 == 0: ans.append(i) else: ans.append(i + (j - nl + 1) // 2) if r == n * (n - 1): ans.append(1) print(" ".join([str(i) for i in ans])) t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) slv(n, l, r)
FUNC_DEF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) L = [0] tt = 2 * (n - 1) for i in range(n): L.append(tt) tt -= 2 L[-1] = 1 temp = 0 ct = r - l + 1 c = 0 tot = 0 for i in range(1, len(L)): if tot + L[i] < l: tot += L[i] else: rem = l - tot p1 = i p2 = rem // 2 if rem % 2 != 0: p2 += 1 temp = 1 p2 += i break if temp == 0: print(p2, end=" ") p2 += 1 if p2 == n + 1: p1 += 1 if p1 == n: p1 = 1 p2 = p1 + 1 c += 1 if p1 == n: p1 = 1 while c < ct: print(p1, end=" ") c += 1 if c == ct: break print(p2, end=" ") c += 1 p2 += 1 if p2 == n + 1: p1 += 1 if p1 == n: p1 = 1 p2 = p1 + 1 print(" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
from sys import stdin input = stdin.readline q = int(input()) for _ in range(q): n, l, r = map(int, input().split()) tab = [-1, 1] cyk = 0 for i in range(n): cyk += 2 * (n - i - 1) tab.append(cyk + 1) tab.append(1012809128301279797787789798789798798) i = 0 start = i while True: if tab[i + 1] > l: start = i break else: i += 1 ind = l dlug = 0 koniec = 0 while True: if koniec: break target = tab[start + 1] while ind < target: if koniec: break if ind == n * (n - 1) + 1: print(1, end=" ") dlug += 1 else: dlug += 1 if ind % 2 == 1: print(i, end=" ") else: print(n - (target - ind - 1) // 2, end=" ") ind += 1 if dlug >= r - l + 1: koniec = 1 start += 1 i += 1 print()
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys d = [] def bins(k, n): l = 0 r = n - 2 while l != r: mid = (l + r + 1) // 2 if d[mid] <= k: l = mid else: r = mid - 1 ex = k - d[l] if ex & 1: f = ex // 2 return f + l + 2 else: return l + 1 for q in range(int(sys.stdin.readline())): n, l, r = [int(j) for j in sys.stdin.readline().split()] mx = n * (n - 1) d = [0] * (n - 1) for i in range(1, n - 1): d[i] = d[i - 1] + 2 * (n - i) ret = [] for i in range(l - 1, r): ret.append(str(bins(0 if i == mx else i, n))) sys.stdout.write(" ".join(ret) + "\n")
IMPORT ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR STRING
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys input = sys.stdin.readline (t,) = map(int, input().split()) def gen(k): return [(k + (i + 1) // 2 if i % 2 else k) for i in range(2 * (n - k))] for _ in range(t): n, s, e = map(int, input().split()) k, sm = 0, 0 while sm < s and k < n - 1: k += 1 sm += 2 * (n - k) if sm < s: k += 1 k2, sm2 = 0, 0 while sm2 < e and k2 < n - 1: k2 += 1 sm2 += 2 * (n - k2) if sm2 < e: k2 += 1 if k == n: print(1) elif k == k2: L = gen(k) tt, tt2 = sm - 2 * (n - k), sm2 - 2 * (n - k2) print(*L[s - tt - 1 : e - tt2]) else: tt, tt2 = sm - 2 * (n - k), sm2 - 2 * (n - k2) L = gen(k)[s - tt - 1 :] L2 = gen(k2)[: e - tt2] for l in range(k + 1, k2): L += gen(l) L += L2 if k2 == n: L.append(1) print(*L)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
t = int(input()) for case_num in range(t): n, l, r = map(int, input().split(" ")) if l == n * (n - 1) + 1: print(1) continue total = 0 unvisited = n - 1 while total < l: total += unvisited * 2 unvisited -= 1 unvisited += 1 total -= unvisited * 2 current = n - unvisited nxt = current + 1 ans = [] while total < r: total += 1 now = current if total % 2 == 1 else nxt if now == nxt: nxt += 1 if nxt > n: current += 1 nxt = current + 1 if current == n: current = 1 if total >= l: ans.append(now) print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
t = int(input()) for i in range(t): n, l, r = map(int, input().split()) if l == n * (n - 1) + 1: print(1) else: x = 1 summa = x * 2 * n rasn = x * (x + 1) while summa - rasn < l: summa += 2 * n rasn = rasn // x * (x + 2) x += 1 x -= 1 summa -= 2 * n rasn = rasn // (x + 2) * x first = x + 1 second = (l - summa + rasn + 1) // 2 + first if (l - summa + rasn) % 2 == 0: print(second, end=" ") if second == n: first += 1 second = first + 1 else: second += 1 ind = l + 1 else: ind = l while ind + 1 <= r: print(first, second, end=" ") if second == n: first += 1 second = first + 1 else: second += 1 ind += 2 if ind == r: if r == n * (n - 1) + 1: print(1) elif second == n: print(first) else: print(first) else: print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys from itertools import accumulate def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**9 + 7 def gen_arr(v): if N - v == 0: return [1] n = (N - v) * 2 res = [0] * n x = v + 1 for i in range(n): if i % 2 == 0: res[i] = v else: res[i] = x x += 1 return res for _ in range(INT()): N, l, r = MAP() l -= 1 ans = [] cur = 0 v = 1 incr = (N - 1) * 2 while cur + incr < l: cur += incr v += 1 incr -= 2 ans = gen_arr(v) ans = ans[l - cur :] ln = r - l v += 1 while len(ans) < ln: ans += gen_arr(v) v += 1 ans = ans[:ln] print(*ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN LIST NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
test = int(input()) for _ in range(test): n, l, r = [int(x) for x in input().split()] start = 1 it = 1 if l == n * (n - 1) + 1: print("1") continue while start < l: start += (n - it) * 2 it += 1 if start != l: it -= 1 start -= (n - it) * 2 a = it b = it + 1 ok = True while start < l: if ok: ok = False else: ok = True b += 1 start += 1 while start <= r: if b == n + 1: a += 1 b = a + 1 if a == n: print("1", end=" ") break if ok: print(a, end=" ") ok = False else: print(b, end=" ") b += 1 ok = True start += 1 print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING WHILE VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING STRING IF VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
R = lambda: map(int, input().split()) t = int(input()) def block(b, pos, n): if pos % 2 == 0: return b return b + (pos + 1) // 2 def binary(k, n): left, right = 1, n + 1 while left <= right: m = (left + right) // 2 if m * (m + 1) >= k > m * (m - 1): return m if m * (m + 1) < k: left = m + 1 if m * (m - 1) >= k: right = m - 1 def whatis(k, n): k = n * (n - 1) + 1 - k if k == 0: return 1 i = binary(k, n) return block(n - i, -k + i * (i + 1), n) for _ in range(t): n, l, r = R() ans = [] for i in range(l, r + 1): ans.append(whatis(i, n)) print(*ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys def data(): return sys.stdin.buffer.readline().strip() out = sys.stdout.write def mdata(): return map(int, data().split()) for t in range(int(data())): n, l, r = mdata() a = l for i in range(1, n + 1): if 2 * (n - i) <= a: a -= 2 * (n - i) else: break cnt = l ans = [] if l % 2 == 0: if a == 0: ans.append(str(n)) else: ans.append(str(i + a // 2)) cnt += 1 k = i + a // 2 + 1 for j in range(i, n): while cnt <= r: ans.append(str(j)) cnt += 1 if cnt > r: break ans.append(str(k)) k += 1 cnt += 1 if k == n + 1: k = j + 2 break if r == n * (n - 1) + 1: ans.append(str(1)) out(" ".join(ans) + "\n")
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP NUMBER BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR STRING
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
for nt in range(int(input())): w, a, b = map(int, input().split()) if w == 2: l = [1, 2, 1] print(*l[a - 1 : b]) continue k = w prev = 0 for j in range(a, b + 1): i = j - prev while k > 1: if i <= 2 * (k - 1): if i % 2: print(w - k + 1, end=" ") else: print(i // 2 + (w - k + 1), end=" ") break else: i -= 2 * (k - 1) prev += 2 * (k - 1) k -= 1 if k == 1: print(1, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER STRING VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
def calc(N, index): if index == 1 or index == N * (N - 1) + 1: return 1 else: wave_l = 1 wave_r = N while wave_r - wave_l > 1: M = (wave_l + wave_r) // 2 if (2 * N - M) * (M - 1) < index: wave_l = M else: wave_r = M wave = wave_l Left = index - (2 * N - wave) * (wave - 1) if Left % 2 != 0: return wave else: return wave + Left // 2 Left = index - (cnt - 1) * (cnt - 2) if Left % 2 == 0: return cnt elif cnt != Left // 2 + 1: return Left // 2 + 1 else: return 1 def solve(): n, l, r = map(int, input().split()) A = [] for i in range(l, r + 1): A.append(calc(n, i)) print(*A) def main(): T = int(input()) for i in range(T): solve() main()
FUNC_DEF IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
T = int(input()) for _ in range(T): n, left, right = map(int, input().split()) l = [0] * n l[1] = 2 * n - 2 for i in range(2, n): l[i] = l[i - 1] - 2 l[-1] += 1 left_n = -1 right_n = -1 acc = 0 for i in range(1, n): if acc + 1 <= left: left_n = i acc += l[i] if right <= acc: right_n = i break ans = [] for i in range(left_n, right_n + 1): now = [] for j in range(l[i]): if j % 2 == 0: now.append(i) elif j == 1: now.append(i + 1) else: now.append(now[-2] + 1) if i == n - 1: now[-1] = 1 ans += now sta = left - sum(l[:left_n]) - 1 print(*ans[sta : sta + right - left + 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
from sys import stdin def allWays(start, verts, done, stack, n): global valid if not valid: return stack.append(start) if len(done) == len(verts): print(stack) stack.pop() valid = False return for x in range(1, n + 1): if start != x and not (start, x) in done: done.add((start, x)) allWays(x, verts, done, stack, n) done.remove((start, x)) stack.pop() def order(n, x): out = [] for y in range(x + 1, n + 1): out.append(x) out.append(y) return out for case in range(int(stdin.readline())): n, l, r = [int(x) for x in stdin.readline().split()] end1 = False if r == n * (n - 1) + 1: end1 = True r -= 1 if l == n * (n - 1) + 1: print(1) else: x = 1 while l > 2 * (n - x): l -= 2 * (n - x) r -= 2 * (n - x) x += 1 out = order(n, x) r -= 2 * (n - x) x += 1 while r > 0: out += order(n, x) r -= 2 * (n - x) x += 1 if end1: out += [1] if r != 0: realOut = out[l - 1 : r] else: realOut = out[l - 1 :] print(" ".join([str(b) for b in realOut]))
FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR VAR LIST NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
for _ in range(int(input())): n, l, r = map(int, input().split()) s = 0 ans = [] for i in range(1, n): gap2 = i t = i if s + (n - i) * 2 >= l: for j in range(l, r + 1): gg = (j - s) // 2 if j % 2 == 1: ans.append(gap2) else: ans.append(gap2 + gg) if gap2 + gg == n: gap2 += 1 s += (n - t) * 2 t += 1 if r == n * (n - 1) + 1: ans[-1] = 1 break else: s += (n - i) * 2 if len(ans) == 0: print(1) else: print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
import sys def calcStartIndex(vertex, n): i = vertex return 1 + 2 * (i - 1) * n - i * i + i def main(): t = int(input()) allans = [] for _ in range(t): n, l, r = readIntArr() startVertex = 1 b = n while b > 0: while startVertex + b <= n and calcStartIndex(startVertex + b, n) <= l: startVertex += b b //= 2 sv = startVertex idx = calcStartIndex(sv, n) ans = [] adder = 1 addTurn = False while idx <= r: if addTurn: curr = sv + adder adder += 1 else: curr = sv if idx >= l: if sv < n: ans.append(curr) else: ans.append(1) addTurn = not addTurn idx += 1 if curr == n: sv += 1 adder = 1 addTurn = False allans.append(ans) multiLineArrayOfArraysPrint(allans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] inf = float("inf") MOD = 10**9 + 7 main()
IMPORT FUNC_DEF ASSIGN VAR VAR RETURN BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
from sys import gettrace, stdin def input(): return stdin.buffer.readline() t = int(input()) for _ in range(t): n, l, r = list(map(int, input().split())) count = 0 i = 1 while count < l: if i == n: i = 1 count += (n - i) * 2 i += 1 i -= 1 count -= (n - i) * 2 j = i + (l - count + 1) // 2 length = r - l + 1 if l % 2 == 0: print(j, end=" ") j += 1 length -= 1 if j > n: i += 1 j = i + 1 while length > 0: if i == n: i = 1 print(i, end=" ") length -= 1 if length <= 0: break print(j, end=" ") length -= 1 j += 1 if j > n: i += 1 j = i + 1 print()
FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ β€” a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) β€” the number of test cases. Next $T$ lines contain test cases β€” one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) β€” the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$.
t = int(input()) for i in range(t): n, l, r = map(int, input().split()) if l == n * (n - 1) + 1: print(1) continue left, right, summ = 0, n, 0 while left != right - 1: mid = (left + right) // 2 tmp = n * mid - mid * (mid + 1) // 2 if 2 * tmp < l: left = mid summ = 2 * tmp else: right = mid beg = summ count = left + 1 ans = [] ind = False for i in range(count, n): for j in range(i + 1, n + 1): ans.append(i) ans.append(j) summ += 2 if summ > r: ind = True break if ind: break ans.append(1) for i in range(l - 1, r): print(ans[i - beg], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR
Chef loves problems about digits and he came up with an interesting one. Chef has given you two integers N and K. You should find the minimum non-negative integer X, such that N + X has at most K distinct digits in its decimal representation. For example, 1231 has three distinct digits and 100 has two distinct digits. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case consists of two space-separated integers N and K, as described in the problem statement. ------ Output Format ------ For each test case, output on a new line the minimum non-negative integer X which satisfies the problem conditions. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10$ ----- Sample Input 1 ------ 9 30 1 56 4 364 2 125 3 37662730 3 41872528 4 73170084 8 90032975 1 7487471 3 ----- Sample Output 1 ------ 3 0 2 0 603 1583 0 9967024 3 ----- explanation 1 ------ Test case $1$: $30$ has $2$ distinct digits which is more than $1$, so we should add $3$ to $30$ to make it equal to $33$ which has only $1$ distinct digit.
from sys import stdin, stdout input = stdin.readline r_int = lambda: int(input()) m_int = lambda: map(int, input().split()) l_int = lambda: list(map(int, input().split())) def solve(): n_int, k = m_int() n = list(map(int, str(n_int))) if len(set(n)) <= k: print(0) return for i, d in enumerate(n): if d == 9: continue used = set(n[:i]) if len(used) > k: break if len(used) < k: d1 = d + 1 used.add(d1) else: d1 = d + 1 while d1 not in used and d1 != 10: d1 += 1 if d1 == 10: continue if len(used) == k: d2 = min(used) else: d2 = 0 m = n.copy() m[i] = d1 for j in range(i + 1, len(n)): m[j] = d2 acc = 0 for x in m: acc = 10 * acc + x print(acc - n_int) def main(): n_cases = r_int() for _ in range(n_cases): solve() main()
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Chef loves problems about digits and he came up with an interesting one. Chef has given you two integers N and K. You should find the minimum non-negative integer X, such that N + X has at most K distinct digits in its decimal representation. For example, 1231 has three distinct digits and 100 has two distinct digits. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case consists of two space-separated integers N and K, as described in the problem statement. ------ Output Format ------ For each test case, output on a new line the minimum non-negative integer X which satisfies the problem conditions. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10$ ----- Sample Input 1 ------ 9 30 1 56 4 364 2 125 3 37662730 3 41872528 4 73170084 8 90032975 1 7487471 3 ----- Sample Output 1 ------ 3 0 2 0 603 1583 0 9967024 3 ----- explanation 1 ------ Test case $1$: $30$ has $2$ distinct digits which is more than $1$, so we should add $3$ to $30$ to make it equal to $33$ which has only $1$ distinct digit.
for _ in range(int(input())): s, k = input().split() n, k = int(s), int(k) mark = [0] * 10 ans = "9" * len(s) for i in range(len(s)): d = ord(s[i]) - ord("0") d1, d2 = d + 1, 0 if sum(mark) < k: if sum(mark) == k - 1 and d1 < 10 and mark[d1] == 0: mark[d1] = 1 while mark[d2] == 0: d2 += 1 mark[d1] = 0 else: while d1 < 10 and mark[d1] == 0: d1 += 1 while d2 < 10 and mark[d2] == 0: d2 += 1 if d1 < 10: ans = min( ans, s[:i] + chr(ord("0") + d1) + chr(ord("0") + d2) * (len(s) - i - 1) ) mark[d] = 1 if sum(mark) > k: break if sum(mark) <= k: ans = s print(int(ans) - n)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Chef loves problems about digits and he came up with an interesting one. Chef has given you two integers N and K. You should find the minimum non-negative integer X, such that N + X has at most K distinct digits in its decimal representation. For example, 1231 has three distinct digits and 100 has two distinct digits. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case consists of two space-separated integers N and K, as described in the problem statement. ------ Output Format ------ For each test case, output on a new line the minimum non-negative integer X which satisfies the problem conditions. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10$ ----- Sample Input 1 ------ 9 30 1 56 4 364 2 125 3 37662730 3 41872528 4 73170084 8 90032975 1 7487471 3 ----- Sample Output 1 ------ 3 0 2 0 603 1583 0 9967024 3 ----- explanation 1 ------ Test case $1$: $30$ has $2$ distinct digits which is more than $1$, so we should add $3$ to $30$ to make it equal to $33$ which has only $1$ distinct digit.
for _ in range(int(input())): n, k = [int(x) for x in input().split()] n_str = str(n) n_len = len(n_str) pfx_set = set() min_pfx_set = ":" min_big = "1" * (n_len + 1) ok_already = True for i in range(n_len): c = n_str[i] if len(pfx_set) == k: cc = ":" for ch in pfx_set: if ch > c and ch < cc: cc = ch if cc != ":": min_big = n_str[:i] + cc + min_pfx_set * (n_len - i - 1) elif c < "9": cc = chr(ord(c) + 1) if cc not in pfx_set and len(pfx_set) == k - 1: min_ch = min(min_pfx_set, cc) else: min_ch = "0" min_big = n_str[:i] + cc + min_ch * (n_len - i - 1) pfx_set.add(c) if c < min_pfx_set: min_pfx_set = c if len(pfx_set) > k: ok_already = False break if ok_already: print(0) else: print(int(min_big) - n)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius. He has a n Γ— n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money. We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it). -----Input----- The first line contains a single integer n (2 ≀ n ≀ 2000). Each of the next n lines contains n integers a_{ij} (0 ≀ a_{ij} ≀ 10^9) β€” description of the chessboard. -----Output----- On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x_1, y_1, x_2, y_2 (1 ≀ x_1, y_1, x_2, y_2 ≀ n), where x_{i} is the number of the row where the i-th bishop should be placed, y_{i} is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right. If there are several optimal solutions, you can print any of them. -----Examples----- Input 4 1 1 1 1 2 1 1 0 1 1 1 0 1 0 0 1 Output 12 2 2 3 2
from sys import stdin input = stdin.buffer.readline n = int(input()) board = [list(map(int, input().split())) for i in range(n)] d1 = [0] * (2 * n - 1) d2 = [0] * (2 * n - 1) for r in range(n): for c in range(n): d1[n - 1 + r - c] += board[r][c] d2[r + c] += board[r][c] best = [-1] * 2 bestv = [None] * 2 for r in range(n): for c in range(n): p = (r + c) % 2 cur = d1[n - 1 + r - c] + d2[r + c] - board[r][c] if cur > best[p]: best[p] = cur bestv[p] = [str(r + 1), str(c + 1)] print(sum(best)) print(" ".join(sum(bestv, [])))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR LIST
Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius. He has a n Γ— n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money. We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it). -----Input----- The first line contains a single integer n (2 ≀ n ≀ 2000). Each of the next n lines contains n integers a_{ij} (0 ≀ a_{ij} ≀ 10^9) β€” description of the chessboard. -----Output----- On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x_1, y_1, x_2, y_2 (1 ≀ x_1, y_1, x_2, y_2 ≀ n), where x_{i} is the number of the row where the i-th bishop should be placed, y_{i} is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right. If there are several optimal solutions, you can print any of them. -----Examples----- Input 4 1 1 1 1 2 1 1 0 1 1 1 0 1 0 0 1 Output 12 2 2 3 2
import sys input = sys.stdin.buffer.readline t = 1 for __ in range(t): a = [] n = int(input()) for i in range(n): b = list(map(int, input().split())) a.append(b) dr = {} di = {} for i in range(n): for j in range(n): dr[i + j] = dr.get(i + j, 0) + a[i][j] di[i + n - j + 1] = di.get(i + n - j + 1, 0) + a[i][j] ind1 = [0] * 2 ind2 = [0] * 2 maxi1 = 0 maxi2 = 0 for i in range(n): for j in range(n): if i + j & 1 == 1: if maxi1 <= dr[i + j] + di[i + n - j + 1] - a[i][j]: maxi1 = dr[i + j] + di[i + n - j + 1] - a[i][j] ind1[0] = i + 1 ind1[1] = j + 1 elif maxi2 <= dr[i + j] + di[i + n - j + 1] - a[i][j]: maxi2 = dr[i + j] + di[i + n - j + 1] - a[i][j] ind2[0] = i + 1 ind2[1] = j + 1 print(maxi1 + maxi2) print(ind1[0], ind1[1], ind2[0], ind2[1])
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius. He has a n Γ— n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money. We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it). -----Input----- The first line contains a single integer n (2 ≀ n ≀ 2000). Each of the next n lines contains n integers a_{ij} (0 ≀ a_{ij} ≀ 10^9) β€” description of the chessboard. -----Output----- On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x_1, y_1, x_2, y_2 (1 ≀ x_1, y_1, x_2, y_2 ≀ n), where x_{i} is the number of the row where the i-th bishop should be placed, y_{i} is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right. If there are several optimal solutions, you can print any of them. -----Examples----- Input 4 1 1 1 1 2 1 1 0 1 1 1 0 1 0 0 1 Output 12 2 2 3 2
from sys import stdin input = stdin.buffer.readline n = int(input()) l = [[*map(int, input().split())] for _ in range(n)] d = {} su = {} s = 0 an = [1, 1, 2, 1] for i in range(n): for j in range(n): d[i - j] = d.get(i - j, 0) + l[i][j] su[i + j] = su.get(i + j, 0) + l[i][j] position = [2, 1, 1, 1] x, y = 0, 0 for i in range(n): for j in range(n): p = d[i - j] + su[i + j] - l[i][j] if (i + j) % 2: if p > x: x = p position[:2] = [i + 1, j + 1] elif p > y: y = p position[2:] = [i + 1, j + 1] print(x + y) print(*position)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius. He has a n Γ— n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money. We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it). -----Input----- The first line contains a single integer n (2 ≀ n ≀ 2000). Each of the next n lines contains n integers a_{ij} (0 ≀ a_{ij} ≀ 10^9) β€” description of the chessboard. -----Output----- On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x_1, y_1, x_2, y_2 (1 ≀ x_1, y_1, x_2, y_2 ≀ n), where x_{i} is the number of the row where the i-th bishop should be placed, y_{i} is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right. If there are several optimal solutions, you can print any of them. -----Examples----- Input 4 1 1 1 1 2 1 1 0 1 1 1 0 1 0 0 1 Output 12 2 2 3 2
from sys import stdin input = stdin.buffer.readline I = lambda: list(map(int, input().split())) n = int(input()) ans = [] for _ in range(n): arr = I() ans.append(arr) topLeft, bottomLeft = {}, {} for i in range(n): for j in range(n): topLeft[i - j] = topLeft.get(i - j, 0) + ans[i][j] bottomLeft[i + j] = bottomLeft.get(i + j, 0) + ans[i][j] mx1, mx2, pos1, pos2 = -1, -1, [-1, -1], [-1, -1] for i in range(n): for j in range(n): if i + j & 1: if mx1 < topLeft[i - j] + bottomLeft[i + j] - ans[i][j]: mx1, pos1 = topLeft[i - j] + bottomLeft[i + j] - ans[i][j], [ i + 1, j + 1, ] elif mx2 < topLeft[i - j] + bottomLeft[i + j] - ans[i][j]: mx2, pos2 = topLeft[i - j] + bottomLeft[i + j] - ans[i][j], [i + 1, j + 1] print(mx1 + mx2) print(pos1[0], pos1[1], pos2[0], pos2[1])
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius. He has a n Γ— n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money. We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it). -----Input----- The first line contains a single integer n (2 ≀ n ≀ 2000). Each of the next n lines contains n integers a_{ij} (0 ≀ a_{ij} ≀ 10^9) β€” description of the chessboard. -----Output----- On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x_1, y_1, x_2, y_2 (1 ≀ x_1, y_1, x_2, y_2 ≀ n), where x_{i} is the number of the row where the i-th bishop should be placed, y_{i} is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right. If there are several optimal solutions, you can print any of them. -----Examples----- Input 4 1 1 1 1 2 1 1 0 1 1 1 0 1 0 0 1 Output 12 2 2 3 2
from sys import stdin input = stdin.buffer.readline I = lambda: list(map(int, input().split())) mat = [] for _ in range(int(input())): mat.append(I()) n = len(mat) def sumDiag(mat): diag_sum = [] diag_sum2 = [] n = len(mat) for i in range(n): s = 0 for j in range(0, n - i): s += mat[j][j + i] diag_sum.append(s) if i != 0: s = 0 for j in range(0, n - i): s += mat[j + i][j] diag_sum2.append(s) return diag_sum2[::-1] + diag_sum def antiDiag(mat): def mirror(mat): for i in range(len(mat)): for j in range(len(mat[0]) // 2): t = mat[i][j] mat[i][j] = mat[i][len(mat[0]) - 1 - j] mat[i][len(mat[0]) - 1 - j] = t return mat mat = mirror(mat) out = sumDiag(mat) mirror(mat) return out[::-1] d1 = sumDiag(mat) d2 = antiDiag(mat) def ret(i, j): return d1[n - 1 - (i - j)] + d2[i + j] - mat[i][j] m1 = 0 m2 = 0 best1 = 1, 1 best2 = 1, 2 for i in range(n): for j in range(n): if (i + j) % 2 == 0 and m1 < ret(i, j): m1 = ret(i, j) best1 = i + 1, j + 1 elif (i + j) % 2 == 1 and m2 < ret(i, j): m2 = ret(i, j) best2 = i + 1, j + 1 print(m1 + m2) print(" ".join(map(str, [best1[0], best1[1], best2[0], best2[1]])))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER VAR FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius. He has a n Γ— n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money. We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it). -----Input----- The first line contains a single integer n (2 ≀ n ≀ 2000). Each of the next n lines contains n integers a_{ij} (0 ≀ a_{ij} ≀ 10^9) β€” description of the chessboard. -----Output----- On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x_1, y_1, x_2, y_2 (1 ≀ x_1, y_1, x_2, y_2 ≀ n), where x_{i} is the number of the row where the i-th bishop should be placed, y_{i} is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right. If there are several optimal solutions, you can print any of them. -----Examples----- Input 4 1 1 1 1 2 1 1 0 1 1 1 0 1 0 0 1 Output 12 2 2 3 2
from sys import stdin input = stdin.buffer.readline I = lambda: list(map(int, input().split())) (n,) = I() l = [] for i in range(n): l.append(I()) d = {} su = {} s = 0 an = [1, 1, 2, 1] for i in range(n): for j in range(n): d[i - j] = d.get(i - j, 0) + l[i][j] su[i + j] = su.get(i + j, 0) + l[i][j] x = 0 y = 0 for i in range(n): for j in range(n): p = d[i - j] + su[i + j] - l[i][j] if (i + j) % 2: if p > x: an[0], an[1] = i + 1, j + 1 x = p elif p > y: an[2], an[3] = i + 1, j + 1 y = p s = x + y print(s) print(*an)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
from sys import stdin def printVal(x): if x <= 9: return x elif x <= 35: return chr(x - 10 + ord("A")) else: return chr(x - 36 + ord("a")) for t in range(int(input())): r, c, k = map(int, input().split()) s = [0] * r for i in range(r): s[i] = stdin.readline() mat = [0] * c ct = 0 for i in range(r): for j in range(c): if s[i][j] == "R": ct += 1 rem = ct % k tot = ct // k till = 0 val = 0 for i in range(r): if i & 1: for j in range(c - 1, -1, -1): if s[i][j] == "R": till += 1 if till > tot + int(rem != 0): val += 1 till = 1 if rem > 0: rem -= 1 mat[j] = printVal(val) for j in range(c): print(mat[j], end="") print() else: for j in range(c): if s[i][j] == "R": till += 1 if till > tot + int(rem != 0): val += 1 till = 1 if rem > 0: rem -= 1 print(printVal(val), end="") print()
FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
chk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for _ in range(int(input())): r, c, k = map(int, input().split()) grid = [] ans = [([""] * c) for i in range(r)] ric = 0 for i in range(r): a = input() grid.append(a) ric += a.count("R") s = ric // k d = ric - s * k ff = (ric - d * (s + 1)) // s t = 0 cur = 0 for i in range(r): if i % 2 == 0: for j in range(c): if grid[i][j] == "R" and d > 0 and cur == s: cur = 0 ans[i][j] = chk[t % 62] t += 1 d -= 1 elif grid[i][j] == "R" and cur == s - 1 and ff > 0 and d == 0: cur = 0 ff -= 1 ans[i][j] = chk[t % 62] t += 1 elif grid[i][j] == "R": cur += 1 ans[i][j] = chk[t % 62] else: ans[i][j] = chk[t % 62] if ff == 0: ans[i][j] = chk[(t - 1) % 62] else: for j in range(c - 1, -1, -1): if grid[i][j] == "R" and d > 0 and cur == s: cur = 0 d -= 1 ans[i][j] = chk[t % 62] t += 1 elif grid[i][j] == "R" and cur == s - 1 and ff > 0 and d == 0: cur = 0 ff -= 1 ans[i][j] = chk[t % 62] t += 1 elif grid[i][j] == "R": cur += 1 ans[i][j] = chk[t % 62] else: ans[i][j] = chk[t % 62] if ff == 0: ans[i][j] = chk[(t - 1) % 62] for i in range(r): print(*ans[i], sep="")
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys def distribute(n, person): q, m = divmod(n, person) if m == 0: return [q] * person else: return [q] * (person - m) + [q + 1] * m input = sys.stdin.readline T = int(input()) CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for _ in range(T): R, C, K = map(int, input().split()) M = [] n = 0 for _ in range(R): r = input()[:-1] n += r.count("R") M.append(r) dst = distribute(n, K) dst_idx = 0 sign = 1 Ans = [] cnt = 0 for m in M: ans = [] for c in m[::sign]: ans.append(CHAR[dst_idx]) if c == "R": cnt += 1 if cnt == dst[dst_idx]: cnt = 0 dst_idx += 1 dst_idx = min(dst_idx, K - 1) Ans.append("".join(ans[::sign])) sign = -sign print("\n".join(Ans))
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP LIST VAR VAR RETURN BIN_OP BIN_OP LIST VAR BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
mp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" imp = 0 t = int(input()) while t > 0: imp = 0 z = input().split() r = int(z[0]) c = int(z[1]) k = int(z[2]) i = 0 j = 0 inp = [] rice = 0 while i < r: a = input() for j in a: if j == "R": rice += 1 inp.append(a) i += 1 i = 0 j = 0 res = [[(0) for _ in range(c)] for _ in range(r)] perChicken = rice // k extra = rice % k curr = perChicken sign = 1 if extra > 0: curr += 1 extra -= 1 while i < r: if inp[i][j] == "R": if curr == 0: curr = perChicken if extra > 0: curr += 1 extra -= 1 imp += 1 curr -= 1 res[i][j] = mp[imp] else: res[i][j] = mp[imp] j += sign * 1 if j >= c: sign = -1 * sign j = c - 1 i += 1 elif j < 0: sign = -1 * sign j = 0 i += 1 for _ in range(r): for __ in range(c): print(res[_][__], end="") print() t -= 1
ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
C = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" T = int(input()) for t in range(T): r, c, k = map(int, input().split()) A = [] for i in range(r): A.append(list(input())) tot = 0 for i in range(r): tot += A[i].count("R") target = [(tot // k + 1 if i < tot % k else tot // k) for i in range(k)] for i in range(r): if i % 2: A[i].reverse() cid = 0 cur = 0 for i in range(r): for j in range(c): if A[i][j] == "R": cur += 1 A[i][j] = C[cid] if cur == target[cid] and cid != k - 1: cid += 1 cur = 0 for i in range(r): if i % 2: A[i].reverse() print("".join(A[i]))
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
t = int(input()) for _ in range(t): r, c, k = map(int, input().split()) arr = [[(0) for j in range(c)] for i in range(r)] chararr = [[(0) for j in range(c)] for i in range(r)] R = 0 for i in range(r): string = input() for j in range(c): arr[i][j] = string[j] if string[j] == "R": R += 1 y = R % k x = k - y string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" rcount = 0 char = 0 for i in range(r): if i % 2 == 0: for j in range(c): if arr[i][j] == "R": rcount += 1 if y > 0: if rcount == R // k + 1: chararr[i][j] = string[char] rcount = 0 char += 1 y -= 1 else: chararr[i][j] = string[char] elif rcount == R // k: chararr[i][j] = string[char] rcount = 0 if x != 1: char += 1 x -= 1 else: chararr[i][j] = string[char] else: for j in range(c - 1, -1, -1): if arr[i][j] == "R": rcount += 1 if y > 0: if rcount == R // k + 1: chararr[i][j] = string[char] rcount = 0 char += 1 y -= 1 else: chararr[i][j] = string[char] elif rcount == R // k: chararr[i][j] = string[char] rcount = 0 if x != 1: char += 1 x -= 1 else: chararr[i][j] = string[char] for i in range(r): for j in range(c): print(chararr[i][j], end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys input = sys.stdin.readline C = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" T = int(input()) for testcases in range(T): r, c, k = map(int, input().split()) MAP = [input().strip() for i in range(r)] ANS = [([None] * c) for i in range(r)] RICE = 0 for ma in MAP: RICE += ma.count("R") base = RICE // k plus_1 = RICE - base * k chicken = 0 c_R = 0 for i in range(r): if i % 2 == 0: for j in range(c): if MAP[i][j] == "R": c_R += 1 ANS[i][j] = C[chicken] if chicken < plus_1 and c_R == base + 1 and chicken < k - 1: c_R = 0 chicken += 1 elif chicken >= plus_1 and c_R == base and chicken < k - 1: c_R = 0 chicken += 1 else: for j in range(c - 1, -1, -1): if MAP[i][j] == "R": c_R += 1 ANS[i][j] = C[chicken] if chicken < plus_1 and c_R == base + 1 and chicken < k - 1: c_R = 0 chicken += 1 elif chicken >= plus_1 and c_R == base and chicken < k - 1: c_R = 0 chicken += 1 for ans in ANS: print("".join(ans))
IMPORT ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
def sym(a): if a <= 10: return str(a - 1) elif a >= 11 and a <= 36: return chr(54 + a) else: return chr(60 + a) t = int(input()) for i in range(t): n, m, k = map(int, input().split()) symbol = "0" num = 1 d = [] t = [["" for j in range(m)] for o in range(n)] c = 0 for j in range(n): r = input() c += r.count("R") d.append(r) minimum = c // k more = c % k - 1 counter = minimum if more >= 0: counter += 1 for j in range(n // 2): for p in range(m): t[2 * j][p] = symbol if d[2 * j][p] == "R": counter -= 1 if counter == 0 and num != k: if more > 0: counter = minimum + 1 more -= 1 else: counter = minimum num += 1 symbol = sym(num) for p in range(m - 1, -1, -1): t[2 * j + 1][p] = symbol if d[2 * j + 1][p] == "R": counter -= 1 if counter == 0 and num != k: if more > 0: counter = minimum + 1 more -= 1 else: counter = minimum num += 1 symbol = sym(num) if n % 2 == 1: for p in range(m): t[-1][p] = symbol if d[-1][p] == "R": counter -= 1 if counter == 0 and num != k: if more > 0: counter = minimum + 1 more -= 1 else: counter = minimum num += 1 symbol = sym(num) for i in t: print(*i, sep="")
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR IF VAR BIN_OP NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
t = int(input()) for _ in range(t): r, c, k = map(int, input().split()) farm = [] rice = 0 chk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for i in range(r): f = input() rice += f.count("R") farm.append(f) n = rice // k rest = rice % k start = 0 cnt = [0] * k for i in range(r): out = "" for j in range(c): if farm[i][j if i % 2 == 0 else c - j - 1] == "R": if cnt[start] < n: cnt[start] += 1 out += chk[start] elif rest > 0: cnt[start] += 1 rest -= 1 out += chk[start] start += 1 else: cnt[start + 1] += 1 out += chk[start + 1] start += 1 else: out += chk[start] print(out if i % 2 == 0 else out[::-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER STRING IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys def count_rice(grid): count = 0 r, c = len(grid), len(grid[0]) for i in range(r): for j in range(c): if grid[i][j] == "R": count += 1 return count t = int(sys.stdin.readline()) l = [] start = 65 for i in range(26): l.append(chr(start + i)) start = 97 for i in range(26): l.append(chr(start + i)) for i in range(10): l.append(chr(48 + i)) for _ in range(t): r, c, k = map(int, sys.stdin.readline().split()) grid = [] for i in range(r): grid.append(list(sys.stdin.readline()[:-1])) rice = count_rice(grid) each = rice // k rem = rice % k i, j = 0, 0 ind, count = 0, 0 z = True while i < r: while j < c and z: if grid[i][j] == "R": if count == each and rem == 0: ind += 1 count = 0 elif count == each and rem != 0: rem -= 1 elif count > each: count = 0 ind += 1 count += 1 grid[i][j] = l[ind] else: grid[i][j] = l[ind] if z: j += 1 else: j -= 1 while j >= 0 and not z: if grid[i][j] == "R": if count == each and rem == 0: ind += 1 count = 0 elif count == each and rem != 0: rem -= 1 elif count > each: count = 0 ind += 1 count += 1 grid[i][j] = l[ind] else: grid[i][j] = l[ind] if z: j += 1 else: j -= 1 if j == c: z = False j = c - 1 else: z = True j = 0 i += 1 for i in range(r): print("".join(x for x in grid[i]))
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys input = sys.stdin.readline t = int(input()) string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for _ in range(t): r, c, k = map(int, input().split()) info = [list(input()) for i in range(r)] ans = [([None] * c) for i in range(r)] cnt_r = 0 for i in range(r): for j in range(c): if info[i][j] == "R": cnt_r += 1 num = [0] * k tmp = cnt_r // k for i in range(k): num[i] += tmp for i in range(cnt_r % k): num[i] += 1 i = 0 j = 0 pos = 0 cnt = 0 while True: if i == r: break if pos == k: ans[i][j] = string[pos - 1] else: if info[i][j] == ".": ans[i][j] = string[pos] else: cnt += 1 ans[i][j] = string[pos] if cnt == num[pos]: pos += 1 cnt = 0 if j == c - 1 and i % 2 == 0: i += 1 elif j == 0 and i % 2 == 1: i += 1 elif i % 2 == 0: j += 1 else: j -= 1 for tmp in ans: print("".join(map(str, tmp)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
chars = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ] t = int(input()) for _ in range(t): r, c, k = map(int, input().split()) board = [list(input()) for _ in range(r)] ans = [([""] * c) for _ in range(r)] cnt = 0 for i in range(r): for j in range(c): if board[i][j] == "R": cnt += 1 pos = 0 tmp = 0 for i in range(r): for j in range(c): if i % 2 == 0: j2 = j elif i % 2 == 1: j2 = c - 1 - j ans[i][j2] = chars[pos] if board[i][j2] == "R": tmp += 1 if pos < cnt % k and tmp >= cnt // k + 1: pos += 1 tmp = 0 elif pos >= cnt % k and tmp >= cnt // k: pos += 1 tmp = 0 if pos == k: pos = k - 1 for i in range(r): print("".join(map(str, ans[i])))
ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
from sys import stdin input = stdin.readline can = [] for i in range(26): can.append(chr(ord("a") + i)) can.append(chr(ord("A") + i)) for i in range(10): can.append(i) t = int(input()) for _ in range(t): mp = [] r, c, k = map(int, input().split()) ans = [(["0"] * c) for _ in range(r)] tot = 0 for i in range(r): mp.append(list(input().split()[0])) for j in range(c): if mp[i][j] == "R": tot += 1 ave = tot // k dis = tot % k dir = 1 i = 0 j = 0 now = 0 tt = 0 need = ave + (now < dis) while i < r: ans[i][j] = can[now] if mp[i][j] == "R": tt += 1 if tt == need: now += 1 now = min(now, k - 1) need = ave + (now < dis) tt = 0 j += dir if j >= c or j < 0: i += 1 dir *= -1 j += dir for i in range(r): print("".join(map(str, ans[i])))
ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys input = sys.stdin.readline Q = int(input()) Query = [] for _ in range(Q): R, C, K = map(int, input().split()) state = [list(input().rstrip()) for _ in range(R)] Query.append((R, C, K, state)) Alp = ( [chr(i) for i in range(97, 97 + 26)] + [chr(i) for i in range(65, 65 + 26)] + [str(i) for i in range(10)] ) for R, C, K, state in Query: count = 0 for r in range(R): for c in range(C): if state[r][c] == "R": count += 1 ans = [([None] * C) for _ in range(R)] Limit = [(count // K + 1 if i < count % K else count // K) for i in range(K)] ind = 0 now = 0 for r in range(R): if r % 2 == 0: seq = list(range(C)) else: seq = list(reversed(range(C))) for c in seq: ans[r][c] = Alp[ind] if state[r][c] == "R": now += 1 if now == Limit[ind] and ind != K - 1: ind += 1 now = 0 for s in ans: print("".join(s))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
def codechickens(m, n, c, r): k = 0 for i in range(r): for j in range(c): if m[i][j] == "R": k += 1 v1 = k // n v2 = k % n s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" numchick = 0 numriceforchick = 0 for i in range(r): s2 = "" if i % 2 == 0: for j in range(c): s2 += s[numchick] if m[i][j] == "R": numriceforchick += 1 if ( numriceforchick == v1 + 1 and numchick < v2 or numriceforchick == v1 and v2 <= numchick < n - 1 ): numriceforchick = 0 numchick += 1 else: for j in range(c - 1, -1, -1): s2 = s[numchick] + s2 if m[i][j] == "R": numriceforchick += 1 if ( numriceforchick == v1 + 1 and numchick < v2 or numriceforchick == v1 and v2 <= numchick < n - 1 ): numriceforchick = 0 numchick += 1 print(s2) q = int(input()) for i in range(q): r, c, k = map(int, input().split()) m = [] for j in range(r): m += list(input().split()) codechickens(m, k, c, r)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
dic = [] for i in range(26): dic.append(chr(ord("a") + i)) for i in range(26): dic.append(chr(ord("A") + i)) for i in range(10): dic.append(str(i)) t = int(input()) while t: li = [] l = [] r, c, k = map(int, input().split()) cr = 0 for i in range(r): li = list(input().strip()) cr += li.count("R") l.append(li) ex = cr % k butt = [] for i in range(k): if ex: butt.append(cr // k + 1) ex -= 1 else: butt.append(cr // k) ind = 0 cou = 0 for i in range(r): if i % 2 == 0: for j in range(c): if ind < len(butt) and cou == butt[ind]: ind += 1 cou = 0 if l[i][j] == "R": l[i][j] = dic[ind] cou += 1 else: l[i][j] = dic[ind] if ind == len(butt) and l[i][j] == dic[ind]: l[i][j] = dic[ind - 1] else: for j in range(c - 1, -1, -1): if ind < len(butt) and cou == butt[ind]: ind += 1 cou = 0 if l[i][j] == "R": l[i][j] = dic[ind] cou += 1 else: l[i][j] = dic[ind] if ind == len(butt) and l[i][j] == dic[ind]: l[i][j] = dic[ind - 1] for i in l: print("".join(i)) t -= 1
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys input = sys.stdin.readline initChickens = [chr(i) for i in range(ord("a"), ord("a") + 26)] initChickens.extend([chr(i) for i in range(ord("A"), ord("A") + 26)]) initChickens.extend([str(i) for i in range(10)]) t = int(input()) for _ in range(t): chickens = initChickens.copy() r, c, k = map(int, input().split()) field = [] rice = 0 for i in range(r): line = list(input().rstrip()) rice += line.count("R") if i & 1: field.extend(line[::-1]) else: field.extend(line) share = rice // k rem = rice % k pnt = 0 for i in range(rem): currRice = 0 chk = chickens.pop() while currRice < share + 1: if field[pnt] == "R": currRice += 1 field[pnt] = chk pnt += 1 for i in range((rice - rem) // share - rem): currRice = 0 chk = chickens.pop() while currRice < share: if field[pnt] == "R": currRice += 1 field[pnt] = chk pnt += 1 while pnt < r * c: field[pnt] = chk pnt += 1 for i in range(r): line = field[i * c : i * c + c] if i & 1: line = line[::-1] print("".join(line))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
q = int(input()) for rw in range(q): r, c, k = list(map(int, input().split())) mat = [list(input()) for i in range(r)] dupa = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0123456789" rice = 0 for i in range(r): for j in range(c): if mat[i][j] == "R": rice += 1 each = rice // k liczby = [] for i in range(k - 1): if (rice - (each + 1)) // (k - i - 1) >= each: liczby.append(each + 1) rice -= each + 1 else: liczby.append(each) rice -= each liczby.append(each) x = 0 y = -1 for i in range(k): rs = 0 while True: if x % 2 == 0: if y < c - 1: y += 1 else: x += 1 elif y > 0: y -= 1 else: x += 1 if mat[x][y] == "R": rs += 1 mat[x][y] = dupa[i] if rs == liczby[i]: break for i in range(r): for j in range(c): if mat[i][j] == ".": mat[i][j] = dupa[k - 1] for i in range(r): print("".join(mat[i]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
char_list = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") def pr(ans): S = "" for R in ans: S += "".join(R) + "\n" print(S[:-1]) T = int(input()) for _ in range(T): r, c, k = map(int, input().split()) cnt = 0 arr = [] for _ in range(r): s = input() arr.append(s) for x in s: if x == "R": cnt += 1 num = cnt // k remain = cnt % k num_arr = [num] * k for i in range(remain): num_arr[i] += 1 ans = [([0] * c) for _ in range(r)] order = [[i, j] for i in range(r) for j in range(c)] for _ in range(r): if _ % 2 == 1: order[_ * c : (_ + 1) * c] = order[_ * c : (_ + 1) * c][::-1] cur_num = 0 cur_c = -1 char = None for x, y in order: if cur_num == 0 and len(num_arr) > 0: cur_num = num_arr.pop(0) cur_c += 1 char = char_list[cur_c] ans[x][y] = char if arr[x][y] == "R": cur_num -= 1 pr(ans)
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) (t,) = I() bl = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] for _ in range(t): r, c, k = I() l = [] ct = tt = 0 for i in range(r): l.append(input().strip()) ct += l[-1].count("R") tt = r * c xt, rt = ct // k, ct % k an = [([0] * c) for i in range(r)] pp = [0] * k ct = cr = 0 x = y = 0 while ct < tt: if l[x][y] == "R": if pp[cr] < xt: pp[cr] += 1 an[x][y] = bl[cr] elif pp[cr] == xt: if rt: rt -= 1 an[x][y] = bl[cr] else: pp[cr + 1] += 1 an[x][y] = bl[cr + 1] cr += 1 else: an[x][y] = bl[cr] if y == c - 1 and x % 2 == 0 or y == 0 and x % 2: x += 1 else: y += -1 if x % 2 else 1 ct += 1 for i in an: print("".join(i))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
def next(i, j, r, c): if i & 1: if j == 0: i += 1 else: j -= 1 elif j == c - 1: i += 1 else: j += 1 return i, j def printMatrix(matrix): s = [[str(e) for e in row] for row in matrix] lens = [max(map(len, col)) for col in zip(*s)] fmt = "\t".join("{{:{}}}".format(x) for x in lens) table = [fmt.format(*row) for row in s] print("_________________") print("\n".join(table)) print("`````````````````") for _ in range(int(input())): r, c, k = map(int, input().split()) mat = [] R = 0 for i in range(r): temp = [x for x in input()] R += temp.count("R") mat.append(temp) ans = [[(0) for i in range(c)] for j in range(r)] string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" st = -1 val = R // k much = [val] * (k - R % k) + [val + 1] * (R % k) value = 0 i = 0 j = 0 for rx in much: rice = rx st += 1 while rice != 0: if mat[i][j] == "R": rice -= 1 ans[i][j] = string[st] i, j = next(i, j, r, c) for i in range(r): for j in range(c): if ans[i][j] == 0: ans[i][j] = string[st] print("".join(ans[i]), end="\n")
FUNC_DEF IF BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
ls = [] for i in range(48, 58): ls.append(chr(i)) for i in range(65, 91): ls.append(chr(i)) for i in range(97, 123): ls.append(chr(i)) T = int(input()) while T: r, c, k = map(int, input().split()) a = [[(0) for j in range(c)] for i in range(r)] count = 0 for i in range(r): b = input() for j in range(c): if b[j] == "R": count += 1 a[i][j] = b[j] x = count // k y = count % k z = 0 count = 0 for i in range(r): for j in range(c): if i % 2 == 0: if a[i][j] == "R": a[i][j] = ls[z] count += 1 if y > 0: if count == x + 1: z += 1 y -= 1 count = 0 elif count == x: z += 1 count = 0 else: a[i][j] = ls[z] elif a[i][c - 1 - j] == "R": a[i][c - 1 - j] = ls[z] count += 1 if y > 0: if count == x + 1: z += 1 y -= 1 count = 0 elif count == x: z += 1 count = 0 else: a[i][c - 1 - j] = ls[z] if z == k: z -= 1 for i in range(r): print("".join(str(x) for x in a[i])) T -= 1
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") sys.setrecursionlimit(10**9) INF = 10**18 MOD = 10**9 + 7 def build_grid(H, W, intv, _type, space=True, padding=False): if space: _input = lambda: input().split() else: _input = lambda: input() _list = lambda: list(map(_type, _input())) if padding: offset = 1 else: offset = 0 grid = list2d(H + offset * 2, W + offset * 2, intv) for i in range(offset, H + offset): row = _list() for j in range(offset, W + offset): grid[i][j] = row[j - offset] return grid for _ in range(INT()): H, W, K = MAP() grid = build_grid(H, W, "*", str, space=False, padding=False) rcnt = 0 for i in range(H): for j in range(W): if grid[i][j] == "R": rcnt += 1 p = rcnt // K q = rcnt % K ans = list2d(H, W, 0) cnt = k = 0 for i in range(H): for j in range(W): x = p + 1 if k < q else p if i % 2 == 1: j = W - j - 1 ans[i][j] = k if grid[i][j] == "R": cnt += 1 if cnt == x and k != K - 1: cnt = 0 k += 1 for i in range(H): for j in range(W): if ans[i][j] < 10: ans[i][j] = str(ans[i][j]) elif 10 <= ans[i][j] < 36: ans[i][j] = chr(ans[i][j] + 87) else: ans[i][j] = chr(ans[i][j] + 29) for i in range(H): print("".join(ans[i]))
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF NUMBER NUMBER IF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
t = int(input()) alp = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" for _ in range(t): count = 0 R, C, k = list(map(int, input().split())) arr = [] for i in range(R): arr.append(input()) final = [["0" for _ in range(C)] for _ in range(R)] total = 0 for i in range(R): for j in range(C): if arr[i][j] == "R": total += 1 h = [0] * k for i in range(0, total): h[i % k] += 1 leftToRight = True curr = 0 for i in range(R): if leftToRight: for j in range(C): final[i][j] = alp[count] if arr[i][j] == "R": curr += 1 if curr == h[count]: if count != len(h) - 1: count += 1 curr = 0 else: for j in range(C - 1, -1, -1): final[i][j] = alp[count] if arr[i][j] == "R": curr += 1 if curr == h[count]: if count != len(h) - 1: count += 1 curr = 0 leftToRight = not leftToRight for i in final: print("".join(i))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys input = lambda: sys.stdin.readline().rstrip() I = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" T = int(input()) for _ in range(T): R, C, K = map(int, input().split()) X = [[(1 if a == "R" else 0) for a in input()] for _ in range(R)] s = sum([sum(x) for x in X]) a = s // K r = s - K * a Y = [a] * (K - r) + [a + 1] * r Y[-1] += 1 k = 0 ANS = [([""] * C) for _ in range(R)] for i in range(R): for j in range(C)[:: -1 if i % 2 else 1]: ANS[i][j] = I[k] if X[i][j] and Y[k]: Y[k] -= 1 if Y[k] == 0: k += 1 for a in ANS: print("".join(a))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for _ in range(int(input())): r, c, k = map(int, input().split()) total = 0 s = [] for l in (sys.stdin.readline() for _ in range(r)): s.append(l.rstrip()) total += l.count("R") ans = [(["*"] * c) for _ in range(r)] min_rice = total // k rice = [min_rice + 1] * (total % k) + [min_rice] * (k - total % k) count = 0 i = 0 x, y = 0, 0 while y < r: if s[y][x] == "R": count += 1 if count > rice[i]: count = 1 i += 1 ans[y][x] = chars[i] x += -1 if y % 2 else 1 if x < 0: y += 1 x = 0 elif x >= c: y += 1 x = c - 1 for l in ans: print("".join(l))
IMPORT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP LIST VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
t = int(input()) ans = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] i = 65 while i < 91: ans.append(chr(i)) i += 1 i = 97 while i < 123: ans.append(chr(i)) i += 1 for z in range(t): r, c, k = map(int, input().split()) ri = 0 arr = [input() for x in range(r)] i = 0 while i < r: j = 0 while j < c: if arr[i][j] == "R": ri += 1 j += 1 i += 1 rc = ri // k rsc = ri % k ba = -1 a = 0 cr = 0 i = 0 j = 0 b = False while i < r: s = "" ba *= -1 while 0 <= j < c: s += ans[a] if arr[i][j] == "R": cr += 1 if b: a += 1 b = False cr = 0 if cr == rc and not b: if rsc > 0: b = True rsc -= 1 else: cr = 0 a += 1 if a >= k: a = k - 1 j += ba j -= ba i += 1 if ba == 1: print(s) else: print(s[::-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR STRING VAR NUMBER WHILE NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
from sys import stdin, stdout T = int(stdin.readline().strip()) def fill(x, y, k, dr, let): while True: if dr == 1: for i in range(y, m): if mt[x][i] == "R": if k == 0: return [x, i, dr] k -= 1 ans[x][i] = let else: for i in range(y, -1, -1): if mt[x][i] == "R": if k == 0: return [x, i, dr] k -= 1 ans[x][i] = let dr = (dr + 1) % 2 if dr == 1: y = 0 else: y = m - 1 x += 1 if x == n: return [x, x, x] lt = ( [chr(ord("0") + i) for i in range(10)] + [chr(ord("a") + i) for i in range(26)] + [chr(ord("A") + i) for i in range(26)] ) for caso in range(T): n, m, g = map(int, stdin.readline().strip().split()) ans = [["" for j in range(m)] for i in range(n)] r = 0 mt = [] for i in range(n): mt.append(stdin.readline().strip()) r += mt[-1].count("R") x = r // g y = -1 z = 0 for i in range(g, -1, -1): y = r - x * i if y >= 0 and y % (x + 1) == 0 and y // (x + 1) == g - i: z = y // (x + 1) y = i break cx = 0 cy = 0 dr = 1 ind = 0 aux = [0, 0, 1] for i in range(g): if i < y: aux = fill(aux[0], aux[1], x, aux[2], lt[i]) else: aux = fill(aux[0], aux[1], x + 1, aux[2], lt[i]) for i in ans: aux = "" for j in i: aux += j stdout.write("%s\n" % aux)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING IF VAR NUMBER RETURN LIST VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING IF VAR NUMBER RETURN LIST VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN LIST VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
t = int(input()) mas = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for asd in range(t): r, c, k = map(int, input().split()) matr = [] suma = 0 for i in range(r): s = input() matr.append([]) for j in range(c): if s[j] == ".": matr[i].append(0) else: matr[i].append(1) suma += 1 if i % 2 == 1: matr[i] = matr[i][::-1] num = [] for i in range(k - suma % k): num.append(suma // k) for i in range(suma % k): num.append(suma // k + 1) kol = 0 h = 0 for i in range(r): s = "" for j in range(c): if matr[i][j] == 1: if kol == num[h]: kol = 0 h += 1 kol += 1 s += mas[h] if i % 2 == 0: print(s) else: print(s[::-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
t = int(input()) for ti in range(t): r, c, k = list(map(int, input().split())) rc = 0 field = [] for i in range(r): s = input() field.append(s) for j in range(c): if s[j] == "R": rc += 1 res = [[(0) for x in range(c)] for y in range(r)] sr = [chr(ord("a") + x) for x in range(26)] sr += [chr(ord("A") + x) for x in range(26)] sr += [chr(ord("0") + x) for x in range(10)] ch_eat = rc // k ch_extra = rc % k charr = [ch_eat] * k for i in range(ch_extra): charr[i] += 1 cc = 0 cn = 0 def process(i, j): global cc, cn if cc >= k: cc -= 1 if field[i][j] == ".": res[i][j] = cc elif cn + 1 >= charr[cc]: res[i][j] = cc cc += 1 cn = 0 else: cn += 1 res[i][j] = cc for i in range(r): if i % 2 == 0: for j in range(c): process(i, j) else: for j in range(c - 1, -1, -1): process(i, j) for i in range(r): res[i] = [sr[x] for x in res[i]] res = ["".join(x) for x in res] print("\n".join(res))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys def zip_sorted(a, b): a, b = zip(*sorted(zip(a, b))) sorted(zip(a, b), key=lambda x: x[1]) return a, b input = sys.stdin.readline I = lambda: list(map(int, input().split())) S = lambda: list(map(str, input())) w = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") (t,) = I() for t1 in range(t): r, c, k = I() b = [] sum1 = 0 for i in range(r): s = S()[:-1] b.append(s) sum1 += s.count("R") y1 = int(sum1 / k) x1 = int(sum1 % k) k1 = 0 sum2 = 0 for i in range(len(b)): if i % 2 == 0: for j in range(len(b[i])): if b[i][j] == "R": b[i][j] = w[k1] sum2 += 1 if x1 > 0: if sum2 < y1 + 1: pass else: sum2 = 0 if k1 != k - 1: k1 += 1 x1 -= 1 elif sum2 < y1: pass else: sum2 = 0 if k1 != k - 1: k1 += 1 else: b[i][j] = w[k1] else: for j in range(len(b[i]) - 1, -1, -1): if b[i][j] == "R": b[i][j] = w[k1] sum2 += 1 if x1 > 0: if sum2 < y1 + 1: pass else: sum2 = 0 if k1 != k - 1: k1 += 1 x1 -= 1 elif sum2 < y1: pass else: sum2 = 0 if k1 != k - 1: k1 += 1 else: b[i][j] = w[k1] for i in range(len(b)): print("".join(b[i]))
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
for _ in range(int(input())): row, col, k = map(int, input().split()) arr = [] for i in range(row): s = input() if i % 2 == 0: temp = [x for x in s] else: temp = [x for x in s[::-1]] arr.extend(temp) ric = arr.count("R") ans = [] c = 65 j = 0 for i in range(1, k + 1): rc = 0 if c == 65 + 26: c = 97 elif c == 97 + 26: c = 48 while True and j < row * col: if arr[j] == "R": mc = ric // k if i <= ric % k: mc += 1 if rc >= mc: c += 1 break rc += 1 ans.append(chr(c)) j += 1 for i in range(row): temp = ans[i * col : (i + 1) * col] if i % 2 != 0: temp = temp[::-1] print(*temp, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR BIN_OP VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
arid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" for _ in range(int(input())): r, c, k = map(int, input().split()) ar = [] count = 0 for i in range(r): ar.append(input()) for j in ar[-1]: if j == "R": count += 1 each = count // k rem = count % k count = 0 tem = 0 for i in range(r): if i % 2 == 0: xx = "" for j in range(0, c, 1): if ar[i][j] == "R": if count <= rem - 1: if tem == each + 1: count += 1 tem = 0 if tem < each + 1: xx += arid[count] tem += 1 elif count > rem - 1: if tem == each: count += 1 tem = 0 if tem < each: xx += arid[count] tem += 1 else: xx += arid[count] print(xx) else: xx = "" for j in range(c - 1, -1, -1): if ar[i][j] == "R": if count <= rem - 1: if tem == each + 1: count += 1 tem = 0 if tem < each + 1: xx += arid[count] tem += 1 elif count > rem - 1: if tem == each: count += 1 tem = 0 if tem < each: xx += arid[count] tem += 1 else: xx += arid[count] print(xx[::-1])
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm. His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm. Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: * Each cell of the farm is assigned to exactly one chicken. * Each chicken is assigned at least one cell. * The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. Input Each test contains multiple test cases. The first line contains the number of test cases T (1 ≀ T ≀ 2 β‹… 10^4). Description of the test cases follows. The first line of each test case contains three integers r, c and k (1 ≀ r, c ≀ 100, 1 ≀ k ≀ 62), representing the size of Long's farm and the number of chickens Long has. Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of r β‹… c over all test cases does not exceed 2 β‹… 10^4. Output For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any. Example Input 4 3 5 3 ..R.. ...R. ....R 6 4 6 R..R R..R RRRR RRRR R..R R..R 5 5 4 RRR.. R.R.. RRR.. R..R. R...R 2 31 62 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Output 11122 22223 33333 aacc aBBc aBBc CbbA CbbA CCAA 11114 22244 32444 33344 33334 abcdefghijklmnopqrstuvwxyzABCDE FGHIJKLMNOPQRSTUVWXYZ0123456789 Note These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice. In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0. <image> In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1. <image> In the third test case, each chicken has 3 cells with rice. <image> In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
import sys def main(): global chickens print = out.append r, c, k = get_list() mat = [list(input()) for _ in range(r)] res = [["a" for _ in range(c)] for _ in range(r)] def next(i, j): if i % 2 == 0 and j + 1 < c: return i, j + 1 elif i % 2 != 0 and j > 0: return i, j - 1 else: return i + 1, j i = j = 0 rice = sum([li.count("R") for li in mat]) party = rice // k extra = rice % k ptr = 0 xtra_used = False while i < r: if mat[i][j] == "R": if party > 0: party -= 1 elif party == 0 and extra > 0 and not xtra_used: extra -= 1 xtra_used = True else: ptr += 1 party = rice // k - 1 xtra_used = False res[i][j] = chickens[ptr] i, j = next(i, j) for li in res: print("".join(li)) chickens = list(map(str, range(10))) for ii in range(ord("a"), ord("z") + 1): chickens.append(chr(ii)) for ii in range(ord("A"), ord("Z") + 1): chickens.append(chr(ii)) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ out = [] get_int = lambda: int(input()) get_list = lambda: list(map(int, input().split())) [main() for _ in range(int(input()))] print(*out, sep="\n")
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR STRING IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST 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 VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
N = int(1000000.0) + 20 n = int(input()) m = [0] * N k = 1000000.0 + 1 for x in map(int, input().split()): m[x] += 1 k = min(k, x) result = 0 while k < N - 1: v = m[k] m[k + 1] += v >> 1 result += v & 1 k += 1 print(result + m[-1])
ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
n = int(input()) l = [0] * (10**6 + 100) for i in map(int, input().split()): l[i] += 1 cur = ans = 0 for i in l: cur += i if cur % 2: ans += 1 cur //= 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
n = int(input()) a = [(0) for i in range(10**6 + 1)] for x in input().split(): a[int(x)] += 1 c = 0 prev = 0 for el in a: prev += el if prev % 2 == 1: c += 1 prev //= 2 while prev > 0: c += prev % 2 prev //= 2 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
def main(): n = int(input()) w = list(map(int, input().split())) bits = [0] * (10**6 + 100) for e in w: bits[e] += 1 cur, res = 0, 0 for e in bits: cur += e if cur % 2: res += 1 cur //= 2 print(res) 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 BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
n = int(input()) weights = list(map(int, input().split())) counter = [0] * (10**6 + 21) for weight in weights: counter[weight] += 1 ans = 0 for i in range(10**6 + 21): weight, count = i, counter[i] if count > 0 and count // 2 > 0: counter[weight + 1] += count // 2 count -= count // 2 * 2 ans += count 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 BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
n = int(input()) l = list(map(int, input().split())) bit = [0] * (10**6 + 101) for i in l: bit[i] = bit[i] + 1 ans = 0 s = 0 for i in bit: s = s + i ans = ans + s % 2 s = s // 2 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 BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
n = int(input()) lis = sorted(map(int, input().split())) ans = c = 0 c = 1 for i in range(1, n): if lis[i] == lis[i - 1]: c += 1 else: while lis[i] != lis[i - 1] and c > 0: if c % 2: ans += 1 c = c // 2 lis[i - 1] += 1 c += 1 while c > 0: if c % 2: ans += 1 c = c // 2 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 FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
def main(): input() l = [0] * 1000001 for w in map(int, input().split()): l[w] += 1 t = rest = 0 for x in l: t += x if t & 1: rest += 1 t >>= 1 print(bin(t).count("1") + rest) main()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps. <image> Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two. Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps. Input The first line of input contains integer n (1 ≀ n ≀ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≀ wi ≀ 106 for each 1 ≀ i ≀ n), the powers of two forming the weights values. Output Print the minimum number of steps in a single line. Examples Input 5 1 1 2 3 3 Output 2 Input 4 0 1 2 3 Output 4 Note In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two. In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
def inc(a, w): n = len(a) i = w while i < n and a[i] == 1: a[i] = 0 i += 1 if i < n: a[i] = 1 else: a.append(1) n = int(input()) x = input().split(" ") w = [int(y) for y in x] a = [0] * 1000024 for t in w: inc(a, t) print(sum(a))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≀ x, y ≀ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≀ n ≀ 100000) β€” the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≀ p_{i} ≀ n) β€” the current structure of the subway. All these numbers are distinct. -----Output----- Print one number β€” the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) ls = [] visited = [(False) for _ in range(n)] cnt = 0 for i in range(n): j = i cnt = 0 while not visited[j]: visited[j] = True cnt += 1 j = p[j] - 1 if 0 < cnt: ls.append(cnt) ls.sort() if 1 < len(ls): ls[-2] += ls[-1] ls.pop() print(sum(map(lambda x: x**2, ls)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≀ x, y ≀ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≀ n ≀ 100000) β€” the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≀ p_{i} ≀ n) β€” the current structure of the subway. All these numbers are distinct. -----Output----- Print one number β€” the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] n = int(input()) p = list(map(int, input().split())) uf = UnionFind(n) for i in range(n): uf.union(i, p[i] - 1) a = [] for i in uf.roots(): a.append([uf.size(i), i]) a = sorted(a, reverse=True) if len(a) >= 2: uf.union(a[0][1], a[1][1]) ans = 0 for i in uf.roots(): ans += uf.size(i) ** 2 print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR VAR FUNC_CALL VAR VAR 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 FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≀ x, y ≀ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≀ n ≀ 100000) β€” the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≀ p_{i} ≀ n) β€” the current structure of the subway. All these numbers are distinct. -----Output----- Print one number β€” the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
import sys n = int(input()) dest = [0] + list(map(int, input().split())) edge = [1] * (n + 1) for i, x in enumerate(dest): if i != x: edge[x] = 0 visit_time = [0] * (n + 1) def solve(i): t = visit_time[i] = 1 while 1: t += 1 i = dest[i] if visit_time[i]: return t - 1, t - visit_time[i] visit_time[i] = t component = [] for i in range(1, n + 1): if edge[i]: component.append(solve(i)) for i in range(1, n + 1): if visit_time[i] == 0: component.append(solve(i)) component.sort(key=lambda x: (x[0], -x[1])) if len(component) == 1: print(component[0][0] ** 2) else: (x1, _), (x2, _) = component.pop(), component.pop() component.append((x1 + x2, x1 + x2)) ans = sum(x * y for x, y in component) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≀ x, y ≀ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≀ n ≀ 100000) β€” the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≀ p_{i} ≀ n) β€” the current structure of the subway. All these numbers are distinct. -----Output----- Print one number β€” the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) a = list(map(int, input().split())) if n == 1: print("1"), exit() if n == 2: print("4"), exit() b = [i for i in range(0, n + 1)] r = [(1) for i in range(0, n + 1)] def find(x): if b[x] == x: return x else: b[x] = find(b[x]) return b[x] for i in range(1, n + 1): x, y = find(i), find(a[i - 1]) if x == y: continue if r[x] >= r[y]: r[x] += r[y] r[y] = 0 b[y] = x else: r[y] += r[x] r[x] = 0 b[x] = y k = 0 for i in range(1, n + 1): k += r[i] ** 2 mx1 = max(r[2], r[1]) mx2 = min(r[2], r[1]) for i in range(3, n + 1): if r[i] > mx1: mx2 = mx1 mx1 = r[i] elif r[i] > mx2: mx2 = r[i] k -= mx1**2 + mx2**2 k += (mx1 + mx2) ** 2 print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≀ x, y ≀ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≀ n ≀ 100000) β€” the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≀ p_{i} ≀ n) β€” the current structure of the subway. All these numbers are distinct. -----Output----- Print one number β€” the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
def dfs(v): cnt = 0 while not vis[v]: vis[v] = True cnt += 1 v = town[v] return cnt n = int(input()) town = list(map(int, input().split())) for i in range(n): town[i] -= 1 cur = [(0) for _ in range(n)] vis = [(False) for _ in range(n)] answer = [0] for i in range(n): answer.append(dfs(i)) answer.sort() print(sum(list(map(lambda x: x * x, answer))) + 2 * answer[-1] * answer[-2])
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER