-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b633b2
commit 08857e2
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from collections import deque | ||
import re | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
def move(node, a, b): | ||
arr = node.split('/') | ||
if len(arr[a]) == 0: | ||
return "" | ||
arr[b] = arr[b] + arr[a][-1] | ||
arr[a] = arr[a][:-1] | ||
return '/'.join(arr) | ||
|
||
|
||
data = [input().split() for _ in range(3)] | ||
for i in range(3): | ||
data[i] = data[i][1] if int(data[i][0]) else "" | ||
data = '/'.join(data) | ||
|
||
visited = set([data]) | ||
queue = deque([(data, 0)]) | ||
while queue: | ||
here, distance = queue.popleft() | ||
|
||
if re.match(r'^A*/B*/C*$', here): | ||
print(distance) | ||
break | ||
|
||
for i in range(3): | ||
for j in range(3): | ||
if i == j: | ||
continue | ||
|
||
there = move(here, i, j) | ||
|
||
if there == "" or there in visited: | ||
continue | ||
|
||
visited.add(there) | ||
queue.append((there, distance + 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
def make_cnt(nums, k): | ||
if k == 0: | ||
return {0: 1} | ||
|
||
if k == 1: | ||
retval = {} | ||
for num in nums: | ||
retval[num] = 1 | ||
return retval | ||
|
||
retval, arr = {}, make_cnt(nums, k // 2) | ||
for a, ac in arr.items(): | ||
for b, bc in arr.items(): | ||
retval[a + b] = (retval.get(a + b, 0) + ac * bc) % MOD | ||
|
||
if k % 2 == 0: | ||
return retval | ||
|
||
retval, arr = {}, retval | ||
for a, ac in arr.items(): | ||
for num in nums: | ||
retval[a + num] = (retval.get(a + num, 0) + ac) % MOD | ||
return retval | ||
|
||
|
||
MOD = 999_983 | ||
K = int(input()) | ||
data = sorted(list(map(int, input()))) | ||
|
||
answer = 0 | ||
cnt = make_cnt(data, K) | ||
for v in cnt.values(): | ||
answer = (answer + v ** 2) % MOD | ||
answer = (2 * answer) % MOD | ||
|
||
minus = 1 | ||
for i in range(K, K + 2): | ||
cnt = make_cnt(data, i // 2) | ||
temp = 0 | ||
for v in cnt.values(): | ||
temp = (temp + v ** 2) % MOD | ||
minus = minus * temp | ||
answer = (answer - minus + MOD) % MOD | ||
print(answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
dx = [0, 1, -1] | ||
dy = [1, 0, 0] | ||
|
||
while True: | ||
N, M = map(int, input().split()) | ||
if N == 0: | ||
break | ||
data = [input() for _ in range(N)] | ||
|
||
x, y = -1, 0 | ||
for i in range(0, M): | ||
if data[0][i] == 'S': | ||
x = i | ||
break | ||
|
||
px, py, pi, ppi = -1, -1, -1, -1 | ||
answer = 1 | ||
while True: | ||
for i in range(3): | ||
nx, ny = x + dx[i], y + dy[i] | ||
if nx < 0 or nx >= M or ny < 0 or ny >= N: | ||
continue | ||
if nx == px and ny == py: | ||
continue | ||
if data[ny][nx] != 'S': | ||
continue | ||
|
||
if pi or (i == 0 or i == ppi): | ||
answer = answer + 1 | ||
else: | ||
ppi = i | ||
|
||
px, py, pi = x, y, i | ||
x, y = nx, ny | ||
break | ||
|
||
else: | ||
break | ||
|
||
print(answer) |