File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ def modular_pow (a , b , c ):
2+ if b == 0 :
3+ return 1
4+ elif b == 1 :
5+ return a % c
6+ else :
7+ half = modular_pow (a , b // 2 , c )
8+ half = half * half % c
9+ if b % 2 == 1 :
10+ half = half * a % c
11+ return half
12+
13+ import sys
14+ input = sys .stdin .read
15+
16+ # 입력을 받습니다.
17+ a , b , c = map (int , input ().strip ().split ())
18+
19+ # 결과를 계산하고 출력합니다.
20+ result = modular_pow (a , b , c )
21+ print (result )
Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .read
3+
4+ def count_paper (x , y , n ):
5+ if n == 1 :
6+ counts [grid [x ][y ]] += 1
7+ return
8+
9+ # 현재 종이가 모두 같은 수로 이루어져 있는지 확인
10+ init_val = grid [x ][y ]
11+ all_same = True
12+ for i in range (x , x + n ):
13+ if not all_same :
14+ break
15+ for j in range (y , y + n ):
16+ if grid [i ][j ] != init_val :
17+ all_same = False
18+ break
19+
20+ # 모두 같은 수로 이루어져 있다면 해당 숫자의 카운트를 증가
21+ if all_same :
22+ counts [init_val ] += 1
23+ else :
24+ # 같은 수로만 이루어져 있지 않다면 9개로 분할하여 각각 재귀적으로 처리
25+ new_size = n // 3
26+ for i in range (3 ):
27+ for j in range (3 ):
28+ count_paper (x + i * new_size , y + j * new_size , new_size )
29+
30+ # 입력 받기
31+ data = sys .stdin .read ().split ()
32+ N = int (data [0 ])
33+ grid = [list (map (int , data [i * N + 1 : (i + 1 ) * N + 1 ])) for i in range (N )]
34+
35+ # 각 숫자 카운트를 저장할 딕셔너리
36+ counts = {- 1 : 0 , 0 : 0 , 1 : 0 }
37+
38+ # 전체 종이에 대해 함수 호출
39+ count_paper (0 , 0 , N )
40+
41+ # 결과 출력
42+ print (counts [- 1 ])
43+ print (counts [0 ])
44+ print (counts [1 ])
Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .read
3+
4+ def compress (x , y , n ):
5+ if n == 1 :
6+ return str (grid [x ][y ])
7+
8+ # 첫 번째 원소를 기준으로 모든 원소가 같은지 확인
9+ initial = grid [x ][y ]
10+ for i in range (x , x + n ):
11+ for j in range (y , y + n ):
12+ if grid [i ][j ] != initial :
13+ # 사분면으로 나누기
14+ next_n = n // 2
15+ top_left = compress (x , y , next_n )
16+ top_right = compress (x , y + next_n , next_n )
17+ bottom_left = compress (x + next_n , y , next_n )
18+ bottom_right = compress (x + next_n , y + next_n , next_n )
19+ return f"({ top_left } { top_right } { bottom_left } { bottom_right } )"
20+
21+ # 모든 원소가 같은 경우
22+ return str (initial )
23+
24+ # 입력 처리
25+ data = input ().strip ().split ()
26+ N = int (data [0 ])
27+ grid = [list (map (int , list (data [i + 1 ]))) for i in range (N )]
28+
29+ # 압축 결과 출력
30+ result = compress (0 , 0 , N )
31+ print (result )
Original file line number Diff line number Diff line change 1+ import sys
2+ sys .setrecursionlimit (10 ** 6 ) #10^6으로 최대 재귀 깊이 설정
3+ input = sys .stdin .readline
4+
5+ def star (l ):
6+ if l == 1 :
7+ return ['*' ]
8+
9+ Stars = star (l // 3 )
10+ L = []
11+
12+ for s in Stars :
13+ L .append (s * 3 ) # N/3 패턴으로 '둘러싼' -> 둘러싸려면 *3
14+ for s in Stars :
15+ L .append (s + ' ' * (l // 3 )+ s ) # N/3의 공백
16+ for s in Stars :
17+ L .append (s * 3 ) # N/3 패턴으로 '둘러싼' -> 둘러싸려면 *3
18+ return L
19+
20+ n = int (input ().strip ())
21+ print ('\n ' .join (star (n )))
Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .read
3+
4+ def count_papers (x , y , size ):
5+ if size == 1 :
6+ if grid [x ][y ] == 0 :
7+ counts [0 ] += 1
8+ else :
9+ counts [1 ] += 1
10+ return
11+
12+ first_color = grid [x ][y ]
13+ same_color = True
14+ for i in range (x , x + size ):
15+ if not same_color :
16+ break
17+ for j in range (y , y + size ):
18+ if grid [i ][j ] != first_color :
19+ same_color = False
20+ break
21+
22+ if same_color :
23+ if first_color == 0 :
24+ counts [0 ] += 1
25+ else :
26+ counts [1 ] += 1
27+ else :
28+ half_size = size // 2
29+ count_papers (x , y , half_size )
30+ count_papers (x , y + half_size , half_size )
31+ count_papers (x + half_size , y , half_size )
32+ count_papers (x + half_size , y + half_size , half_size )
33+
34+ # 입력 처리
35+ data = sys .stdin .read ().strip ().split ()
36+ N = int (data [0 ])
37+ grid = []
38+ index = 1
39+ for i in range (N ):
40+ row = list (map (int , data [index :index + N ]))
41+ grid .append (row )
42+ index += N
43+
44+ # 하얀색(0)과 파란색(1) 색종이의 개수를 저장할 리스트
45+ counts = [0 , 0 ]
46+
47+ # 전체 종이에 대해 함수 호출
48+ count_papers (0 , 0 , N )
49+
50+ # 결과 출력
51+ print (counts [0 ])
52+ print (counts [1 ])
You can’t perform that action at this time.
0 commit comments