結果
| 問題 |
No.1330 Multiply or Divide
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-08 22:55:46 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 208 ms / 2,000 ms |
| コード長 | 1,317 bytes |
| コンパイル時間 | 211 ms |
| コンパイル使用メモリ | 82,508 KB |
| 実行使用メモリ | 117,940 KB |
| 最終ジャッジ日時 | 2025-06-20 01:22:18 |
| 合計ジャッジ時間 | 7,151 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 51 |
ソースコード
# import sys
# input = sys.stdin.readline
def mp():return map(int,input().split())
def lmp():return list(map(int,input().split()))
import math
import bisect
from copy import deepcopy as dc
from itertools import accumulate
from collections import Counter, defaultdict, deque
def ceil(U,V):return (U+V-1)//V
def modf1(N,MOD):return (N-1)%MOD+1
n,m,p = mp()
a = lmp()
ac = dc(a)
if m < max(a):
print(1)
exit()
ag = []
for i in range(n):
if math.gcd(a[i], p) == 1 and a[i] != 1:
ag.append((a[i],1))
else:
cnt = 0
while True:
if a[i] % p == 0:
cnt += 1
a[i] //= p
else:
break
if a[i] != 1:
ag.append((a[i],1+cnt))
if not ag:
print(-1)
exit()
d = defaultdict(lambda:0)
for i in range(len(ag)):
d[ag[i][1]] = max(d[ag[i][1]], ag[i][0])
d = list(d.items()) # 操作回数 かける数
stack = [(1,0)] #今の数 操作回数
ans = int(1e10)
# print(d)
while stack:
now = stack.pop()
# print(now)
if now[0] > m:
ans = min(ans,now[1])
# print(now)
continue
if ceil(m+1,max(ac)) <= now[0]:
ans = min(ans,now[1]+1)
# print(now)
continue
for i,j in d:
stack.append((now[0]*j, now[1]+i))
print(ans)