티스토리 뷰
문제 링크: https://www.acmicpc.net/problem/32242
문제 풀이
폴라드 로, 정수론
처음에는 서브테스크로 나눠서 생각을 해봤는데,
1. 인 경우
1-1.
1-2.
1-3.
1-4.
2. 인 경우
2-1.
당연하겠지만
2-2.
이 문제의 가장 핵심인 문제인데,
가 된다. 여기서 알 수 있는 사실은
2-2-1.
즉
2-2-2.
2-1의 경우와 다르지 않지만 조금 더 조건이 많은데,
따라서
코드
| from math import gcd | |
| from random import * | |
| import sys | |
| input = sys.stdin.readline | |
| prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41] # n < 2^64 | |
| def miller_rabin(n, p): | |
| if n%p == 0: return True | |
| d = n-1 | |
| while d%2==0: d >>= 1 | |
| temp = pow(p, d, n) | |
| if temp == 1 or temp == n-1: return True | |
| while d*2 < n-1: | |
| d *= 2 | |
| if pow(p, d, n) == n-1: return True | |
| return False | |
| def prime_chk(n): | |
| # for i in range(3): # 2^32 | |
| # if n == prime[i]: return True | |
| # if not miller_rabin(n, prime[i]): return False | |
| # return True | |
| for i in range(13): # 2^64 | |
| if n == prime[i]: return True | |
| if not miller_rabin(n, prime[i]): return False | |
| return True | |
| def pollard_rho(n): | |
| if prime_chk(n): return n | |
| if n == 1: return 1 | |
| a = b = randint(2, n) | |
| c, d = randint(1, n), 1 | |
| while True: | |
| a = ((a*a)%n+c%n)%n | |
| for _ in range(2): b = ((b*b)%n+c%n)%n | |
| d = gcd(abs(a-b), n) | |
| if d == n: return pollard_rho(n) | |
| if d != 1: break | |
| if prime_chk(d): return d | |
| else: return pollard_rho(d) | |
| A, B, C, D = map(int, input().split()) | |
| if A == 0: | |
| if B != 0 and C != 0: | |
| x = gcd(B, C) | |
| if -D%x == 0: print("INFINITY") | |
| else: print(0) | |
| elif B == 0 and C != 0: | |
| if -D%C == 0: print("INFINITY") | |
| else: print(0) | |
| elif B != 0 and C == 0: | |
| if -D%B == 0: print("INFINITY") | |
| else: print(0) | |
| else: | |
| if D == 0: print("INFINITY") | |
| else: print(0) | |
| else: | |
| if B == 0 and C == 0: | |
| if D == 0: print("INFINITY") | |
| else: | |
| if D%A == 0: | |
| nn = n = -D//A | |
| n = abs(n) | |
| frac = {} | |
| while n % 2 == 0: | |
| n //= 2 | |
| if 2 in frac: frac[2] += 1 | |
| else: frac[2] = 1 | |
| while n > 1: | |
| div = pollard_rho(n) | |
| if div in frac: frac[div] += 1 | |
| else: frac[div] = 1 | |
| n //= div | |
| divs = [1] | |
| for pr, exp in frac.items(): | |
| new_divs = [] | |
| for d in divs: | |
| for i in range(1, exp+1): | |
| new_divs.append(d*(pr**i)) | |
| divs.extend(new_divs) | |
| ans = [] | |
| for i in divs: | |
| ans.append((-i, -nn//i)) | |
| ans.append((i, nn//i)) | |
| print(len(ans)) | |
| for i in sorted(ans): | |
| print(*i) | |
| else: | |
| print(0) | |
| else: | |
| if B*C == A*D: | |
| if C%A == 0: print("INFINITY") | |
| elif B%A == 0: print("INFINITY") | |
| else: print(0) | |
| else: | |
| x = n = B*C-A*D | |
| n = abs(n) | |
| frac = {} | |
| while n % 2 == 0: | |
| n //= 2 | |
| if 2 in frac: frac[2] += 1 | |
| else: frac[2] = 1 | |
| while n > 1: | |
| div = pollard_rho(n) | |
| if div in frac: frac[div] += 1 | |
| else: frac[div] = 1 | |
| n //= div | |
| divs = [1] | |
| for pr, exp in frac.items(): | |
| new_divs = [] | |
| for d in divs: | |
| for i in range(1, exp+1): | |
| new_divs.append(d*(pr**i)) | |
| divs.extend(new_divs) | |
| ddivs = [] | |
| for i in divs: | |
| ddivs.append((i, x//i)) | |
| ddivs.append((-i, x//(-i))) | |
| ans = [] | |
| for i, j in ddivs: | |
| if (i-C)%A == 0 and (j-B)%A == 0: | |
| ans.append(((i-C)//A, (j-B)//A)) | |
| print(len(ans)) | |
| for i in sorted(ans): | |
| print(*i) |
'BOJ' 카테고리의 다른 글
| 골드 이상 풀이 정리 1주차 (0) | 2024.10.27 |
|---|---|
| [BOJ][Python] 백준 32454번 - Fibonacci Lucky Numbers (0) | 2024.10.25 |
| 랜덤 마라톤 14주차 (0) | 2024.09.05 |
| [BOJ][Python] 백준 9011번 - 순서 (0) | 2022.08.05 |
| [BOJ][Python] 백준 16973번 - 직사각형 탈출 (0) | 2022.08.05 |
- Total
- Today
- Yesterday
- Python
- DP
- backtracking
- Topological Sorting
- 시뮬레이션
- BFS
- 브루트포스
- 다이나믹 프로그래밍
- MST
- 구현
- 백트래킹
- Implementation
- set
- 볼록 껍질
- 그리디
- 위상 정렬
- 정렬
- Brute Force
- convex hull
- 최소 신장 트리
- BOJ
- 파이썬
- TEXT
- Sorting
- math
- 집합과 맵
- 너비 우선 탐색
- 수학
- Simulation
- greedy
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
