ARC106 A - 106
備忘録
問題
回答
import sys import os import math import bisect import itertools import collections import heapq import queue import array import time # 時々使う # from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall # from decimal import Decimal # from collections import defaultdict, deque # 再帰の制限設定 sys.setrecursionlimit(10000000) def ii(): return int(sys.stdin.buffer.readline().rstrip()) def il(): return list(map(int, sys.stdin.buffer.readline().split())) def fl(): return list(map(float, sys.stdin.buffer.readline().split())) def iln(n): return [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)] def iss(): return sys.stdin.buffer.readline().decode().rstrip() def sl(): return list(map(str, sys.stdin.buffer.readline().decode().split())) def isn(n): return [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)] def lcm(x, y): return (x * y) // math.gcd(x, y) MOD = 10 ** 9 + 7 INF = float('inf') def main(): if os.getenv("LOCAL"): sys.stdin = open("input.txt", "r") N = ii() for i in range(1, 38): for j in range(1, 26): if N == 3 ** i + 5 ** j: print(i, j) exit() else: print(-1) if __name__ == '__main__': main()
考え方
3の累乗が取りうる範囲と5の累乗が取りうる範囲を全探索します。
- 回答概要
A
が取りうる範囲は1 <= A <= 37
、B
が取りうる範囲は1 <= B <= 25
A, B
は制約で正の整数となるので、ゼロを含めないことに注意
A, B
の取りうる範囲について考えます。
制約からN
は最大10 ** 18
なので、log
を用いて求めると
print(math.log(10 ** 18, 3)) # 37.726.... print(math.log(10 ** 18, 5)) # 25.752....
となります。
つまり、A
は38からN
の最大値を上回るので、最大37となり、
B
は26からN
の最大値を上回るので、最大26となります。
あとはこの範囲を全探索し、条件を満たすA, B
が存在するか確かめます。
ただし、制約からA, B
は正の整数であるため、ゼロを含めないことに注意が必要です。