File tree Expand file tree Collapse file tree 3 files changed +214
-0
lines changed Expand file tree Collapse file tree 3 files changed +214
-0
lines changed Original file line number Diff line number Diff line change
1
+ import sys
2
+ input = sys .stdin .readline
3
+
4
+ prime_num = [True ]* 2000
5
+ prime_num [0 ],prime_num [1 ]= False ,False
6
+ def getprime ():
7
+ next_num = 2
8
+ while next_num <= 1000 :
9
+ if prime_num [next_num ]:
10
+ n = next_num * 2
11
+ while n < 2000 :
12
+ prime_num [n ]= False
13
+ n += next_num
14
+ next_num += 1
15
+ getprime ()
16
+ N = int (input ())
17
+ arr = list (map (int ,input ().split ()))
18
+
19
+ #각 숫자끼리 연결시 소수되는 경우 네트워크
20
+ #첫번째 숫자와 연결되는 숫자 각각 빼면서 이분탐색
21
+
22
+ adj = [[] for _ in range (N )]
23
+ for idx in range (N - 1 ):
24
+ j = arr [idx ]
25
+ for idx2 in range (idx + 1 ,N ):
26
+ i = arr [idx2 ]
27
+ if prime_num [i + j ]:
28
+ adj [idx ].append (idx2 )
29
+ adj [idx2 ].append (idx )
30
+ ans = []
31
+ def dfs (idx ):
32
+ global n
33
+ visited [idx ]= True
34
+ for nx in adj [idx ]:
35
+ if nx == n or nx == 0 :continue
36
+ if p [nx ]== - 1 or (not visited [p [nx ]] and dfs (p [nx ])):
37
+ p [nx ]= idx
38
+ return True
39
+ return False
40
+
41
+ for n in adj [0 ]:
42
+ p = [- 1 ]* N
43
+ #0,n제외
44
+ tmp = 0
45
+ for i in range (1 ,N ):
46
+ visited = [False ]* N
47
+ if i == n :continue
48
+ if dfs (i ):tmp += 1
49
+ if tmp == N - 2 :ans .append (arr [n ])
50
+ if ans :
51
+ print (' ' .join (map (str ,sorted (ans ))))
52
+ else :
53
+ print (- 1 )
Original file line number Diff line number Diff line change
1
+ import sys
2
+ input = sys .stdin .readline
3
+
4
+
5
+ def sol ():
6
+ N , M , K = map (int ,input ().split ())
7
+ if N - M + 1 < K :
8
+ return [- 1 ]
9
+ else :
10
+ if M >= K :
11
+ arr = [n for n in range (N - M + 1 ,N + 1 )]
12
+ #남은수 N-M
13
+ N -= M
14
+ #증가수열 K-1개 더만들기
15
+ K -= 1
16
+ if K == 0 :
17
+ if N == 0 :
18
+ return arr
19
+ else :
20
+ return [- 1 ]
21
+ num = N // K
22
+ if num > M :
23
+ return [- 1 ]
24
+ #길이는 num(남은 수 N / K)
25
+ while N :
26
+ tmp = []
27
+ for _ in range (num ):
28
+ tmp .append (N )
29
+ N -= 1
30
+ K -= 1
31
+ if K == 0 :
32
+ while N :
33
+ tmp .append (N )
34
+ N -= 1
35
+ arr .extend (tmp [::- 1 ])
36
+ tmp = []
37
+ return arr
38
+ else :
39
+ arr = [n for n in range (K ,0 ,- 1 )]
40
+ M -= 1
41
+ #감소수열 M개 더만들기
42
+ if M == 0 :
43
+ if N == K :
44
+ return arr
45
+ else :
46
+ return [- 1 ]
47
+ num = (N - K )// M
48
+ if num > K :
49
+ return [- 1 ]
50
+ i = K + 1
51
+ while i <= N :
52
+ tmp = []
53
+ for _ in range (num ):
54
+ tmp .append (i )
55
+ i += 1
56
+ M -= 1
57
+ if M == 0 :
58
+ while i <= N :
59
+ tmp .append (i )
60
+ i += 1
61
+ arr .extend (tmp [::- 1 ])
62
+ tmp = []
63
+ return arr
64
+ print (' ' .join (map (str , sol ())))
Original file line number Diff line number Diff line change
1
+ import sys
2
+ input = sys .stdin .readline
3
+
4
+ N , M = map (int ,input ().split ())
5
+ arr = [list (map (int ,input ().split ())) for _ in range (N )]
6
+ ans = ""
7
+ def findminyx ():
8
+ m_list = []
9
+ for y ,t in enumerate (arr ,1 ):
10
+ for x ,num in enumerate (t ,1 ):
11
+ m_list .append ([num ,y ,x ])
12
+ for _ ,y ,x in sorted (m_list ):
13
+ if (y % 2 )^ (x % 2 ):
14
+ return y ,x
15
+ if N % 2 :
16
+ for i in range (N - 1 ):
17
+ if i % 2 == 0 :
18
+ ans += 'R' * (M - 1 )
19
+ else :
20
+ ans += 'L' * (M - 1 )
21
+ ans += "D"
22
+ ans += 'R' * (M - 1 )
23
+ elif M % 2 :
24
+ for i in range (M - 1 ):
25
+ if i % 2 == 0 :
26
+ ans += 'D' * (N - 1 )
27
+ else :
28
+ ans += 'U' * (N - 1 )
29
+ ans += "R"
30
+ ans += "D" * (N - 1 )
31
+ else :
32
+ y ,x = findminyx ()
33
+ if y % 2 == 0 :
34
+ #1~x-1까지
35
+ for i in range (x - 1 ):
36
+ if i % 2 == 0 :
37
+ ans += 'D' * (N - 1 )+ 'R'
38
+ else :
39
+ ans += 'U' * (N - 1 )+ 'R'
40
+ #현재 1,x
41
+ #1~y-1까지
42
+ for i in range (y - 2 ):
43
+ if i % 2 == 0 :
44
+ ans += 'R' * (M - x )+ 'D'
45
+ else :
46
+ ans += 'L' * (M - x )+ 'D'
47
+ if y == N :
48
+ for i in range (M - x ):
49
+ if i % 2 == 0 :ans += 'RD'
50
+ else :ans += 'RU'
51
+ else :
52
+ ans += 'R' * (M - x )+ 'D' + 'L' * (M - x - 1 )+ 'D'
53
+ for i in range (N - y - 1 ):
54
+ if i % 2 == 0 :ans += "LD"
55
+ else :ans += "RD"
56
+ ans += 'R'
57
+ for i in range (M - x - 1 ):
58
+ if i % 2 == 0 :ans += 'R' + 'U' * (N - y - 1 )
59
+ else :ans += 'R' + 'D' * (N - y - 1 )
60
+ #x가 짝
61
+ else :
62
+ #print('hi')
63
+ for i in range (y - 1 ):
64
+ if i % 2 == 0 :ans += 'R' * (M - 1 )+ 'D'
65
+ else :ans += 'L' * (M - 1 )+ 'D'
66
+ for i in range (x - 2 ):
67
+ if i % 2 == 0 :ans += 'D' * (N - y )+ 'R'
68
+ else :ans += 'U' * (N - y )+ 'R'
69
+ if x == M :
70
+ for i in range (N - y ):
71
+ if i % 2 == 0 :ans += 'DR'
72
+ else :ans += 'DL'
73
+ else :
74
+ ans += 'D' * (N - y )+ 'R' + 'U' * (N - y - 1 )+ 'R'
75
+ for i in range (M - x - 1 ):
76
+ if i % 2 == 0 :ans += 'UR'
77
+ else :ans += 'DR'
78
+ ans += 'D'
79
+ for i in range (N - y - 1 ):
80
+ if i % 2 == 0 :ans += 'D' + 'L' * (M - x - 1 )
81
+ else :ans += 'D' + 'R' * (M - x - 1 )
82
+ print (ans )
83
+
84
+ # def check(s):
85
+ # y,x=0,0
86
+ # visited = [[0]*M for _ in range(N)]
87
+ # visited[0][0]=1
88
+ # res = arr[y][x]
89
+ # for c in s:
90
+ # if c=='R':x+=1
91
+ # elif c=='L':x-=1
92
+ # elif c=='U':y-=1
93
+ # else:y+=1
94
+ # res+=arr[y][x]
95
+ # visited[y][x]=1
96
+ # print('\n'.join(map(str,visited)))
97
+ # check(ans)
You can’t perform that action at this time.
0 commit comments