-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc276_d.py
More file actions
94 lines (85 loc) · 2.66 KB
/
Copy pathabc276_d.py
File metadata and controls
94 lines (85 loc) · 2.66 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
###############################################
問題文
正整数列A=(a1,a2,…,aN)が与えられます。
あなたは以下の操作のうち1つを選んで行うことを0回以上何度でも繰り返せます。
1≤i≤Nかつaiが2の倍数であるような整数iを選び、aiをai//2に置き換える
1≤i≤Nかつaiが3の倍数であるような整数iを選び、aiをai//3に置き換える
あなたの目標はAがa1=a2=…=aNを満たす状態にすることです。
目標を達成するために必要な操作の回数の最小値を求めてください。
ただし、どのように操作を行っても目標を達成できない場合、代わりに-1と出力してください。
制約
2≤N≤1000
1≤ai≤10**9
入力はすべて整数
###############################################
入力
N
a1 a2 ... aN
###############################################
[titia AC]
import sys
input = sys.stdin.readline
from math import gcd
N=int(input())
A=list(map(int,input().split()))
GCD=0
for a in A:
GCD=gcd(a,GCD)
ANS=0
for a in A:
x=a//GCD
while x>1:
if x%2==0:
ANS+=1
x//=2
elif x%3==0:
x//=3
ANS+=1
else:
print(-1)
exit()
print(ANS)
###############################################
最重要なポイント:
あなたの目標はAがa1=a2=…=aNを満たす状態にすることです。
必ずしも、素数にまで破り続ける必要はない
問題の意味を取り違えた時点でWA決定
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
[my WA25]
N=int(input())
A=list(map(int,input().split()))
def count_powers(n: int):
count2, count3 = 0, 0
while n % 2 == 0:
n //= 2
count2 += 1
while n % 3 == 0:
n //= 3
count3 += 1
return count2, count3, n # n は残りの部分(2,3 以外)
B=[]
C2=[]
C3=[]
for a in A:
c2,c3,n=count_powers(a)
C2+=[c2]
C3+=[c3]
B+=[n]
t=max(C2)+max(C3)
if len(set(B))==1:
print(t)
else:
print(-1)
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################