-
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
e4c91fe
commit 079a09e
Showing
8 changed files
with
152 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,15 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
N = int(input()) | ||
data = [list(map(int, input().split())) for _ in range(N)] | ||
dp = [data[i][:] for i in range(N)] | ||
|
||
for i in range(N): | ||
for j in range(N): | ||
for k in range(N): | ||
dp[j][k] = max(dp[j][k], dp[j][i] * dp[i][k]) | ||
|
||
for arr in dp: | ||
print(*arr, sep=' ') |
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,33 @@ | ||
from collections import deque | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
N, M = map(int, input().split()) | ||
edges = [[] for _ in range(N + 1)] | ||
for i, x in enumerate(list(map(int, input().split()))): | ||
if x != -1: | ||
edges[i + 1].append(x) | ||
edges[x].append(i + 1) | ||
|
||
nodes = [0] * (N + 1) | ||
for _ in range(M): | ||
i, w = map(int, input().split()) | ||
nodes[i] = nodes[i] + w | ||
|
||
visited = [False] * (N + 1) | ||
visited[1] = True | ||
|
||
queue = deque([(1, 0)]) | ||
while queue: | ||
here, cost = queue.popleft() | ||
nodes[here] = nodes[here] + cost | ||
|
||
for there in edges[here]: | ||
if visited[there]: | ||
continue | ||
|
||
visited[there] = True | ||
queue.append((there, nodes[here])) | ||
|
||
print(*nodes[1:], sep=' ') |
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,29 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
def get(idx): | ||
here = idx | ||
while here != union[here]: | ||
here = union[here] | ||
root = here | ||
|
||
here = idx | ||
while here != union[here]: | ||
here, union[here] = union[here], root | ||
return root | ||
|
||
|
||
N, M = map(int, input().split()) | ||
union = [i for i in range(N + 1)] | ||
for _ in range(M): | ||
o, a, b = map(int, input().split()) | ||
|
||
if o == 0: | ||
union[get(a)] = get(b) | ||
|
||
if o == 1: | ||
if get(a) == get(b): | ||
print("YES") | ||
else: | ||
print("NO") |
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,29 @@ | ||
import sys | ||
sys.stdin = open("1.txt") | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
def max_sum(idx): | ||
answer, psum = float('inf'), 0 | ||
for i, num in enumerate(data): | ||
if i < idx: | ||
psum = psum + num | ||
if i == idx - 1: | ||
answer = psum | ||
continue | ||
psum = psum + num - data[i - idx] | ||
answer = max(answer, psum) | ||
return answer | ||
|
||
|
||
N, S = map(int, input().split()) | ||
data = list(map(int, input().split())) | ||
|
||
s, e = 1, N + 1 | ||
while s < e: | ||
m = (s + e) // 2 | ||
if max_sum(m) >= S: | ||
e = m | ||
else: | ||
s = m + 1 | ||
print(e if e <= N else 0) |
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,5 @@ | ||
N = int(input()) | ||
dp = (1, 1) | ||
for i in range(N): | ||
dp = (dp[1] % 15746, (dp[0] + dp[1]) % 15746) | ||
print(dp[0]) |
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,12 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
N = int(input()) | ||
data = [list(map(int, input().split())) for _ in range(N)] | ||
dp = [0] * (N + 1) | ||
|
||
for i in range(N): | ||
for j in range(i, -1, -1): | ||
dp[j] = max(dp[j - 1], dp[j]) + data[i][j] | ||
print(max(dp)) |
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,18 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
is_prime = [True] * 10001 | ||
for i in range(2, 10001): | ||
if not is_prime[i]: | ||
continue | ||
for j in range(2 * i, 10001, i): | ||
is_prime[j] = False | ||
|
||
T = int(input()) | ||
for _ in range(T): | ||
N = int(input()) | ||
for i in range(N // 2, 1, -1): | ||
if is_prime[i] and is_prime[N - i]: | ||
print(i, N - i, sep=' ') | ||
break |
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,11 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
dp = [0, 1, 1, 1, 2, 2] + [0] * (95) | ||
for i in range(6, 101): | ||
dp[i] = dp[i - 1] + dp[i - 5] | ||
|
||
T = int(input()) | ||
for _ in range(T): | ||
print(dp[int(input())]) |