【AtCoder】ABC201 C - Secret Number

<解き方1>

bit探索?全探索?全然分からないから、まずは具体的に書き出そう→全部書き出せないこともない??という無茶苦茶な戦法ですが、なんとWA×1なんですね…。法則もなんとなく見えてくる。

画像1

s=input()

#0になる時
if s.count("o")>4 or s.count("x")==10:
ans=0

#oが4個の時
elif s.count("o")==4:
ans=24

#oが3個の時
elif s.count("o")==3:
if s.count("x")==7:
ans=36
elif s.count("x")==6:
ans=36+24
elif s.count("x")==5:
ans=36+24*2
elif s.count("x")==4:
ans=36+24*3
elif s.count("x")==3:
ans=36+24*4
elif s.count("x")==2:
ans=36+24*5
elif s.count("x")==1:
ans=36+24*6
else:
ans=36+24*7

#oが2個の時
elif s.count("o")==2:
if s.count("x")==8:
ans=14
elif s.count("x")==7:
ans=14+36
elif s.count("x")==6:
ans=14+36*2+24
elif s.count("x")==5:
ans=14+36*3+24*3
elif s.count("x")==4:
ans=14+36*4+24*6
elif s.count("x")==3:
ans=14+36*5+24*10
elif s.count("x")==2:
ans=14+36*6+24*15
elif s.count("x")==1:
ans=14+36*7+24*21
else:
ans=14+36*8+24*28
#oが1個の時
elif s.count("o")==1:
if s.count("x")==9:
ans=1
elif s.count("x")==8:
ans=1+14
elif s.count("x")==7:
ans=1+14*2+36
elif s.count("x")==6:
ans=1+14*3+36*3+24
elif s.count("x")==5:
ans=1+14*4+36*6+24*4
elif s.count("x")==4:
ans=1+14*5+36*10+24*10
elif s.count("x")==3:
ans=1+14*6+36*15+24*20
elif s.count("x")==2:
ans=1+14*7+36*21+24*35
elif s.count("x")==1:
ans=1+14*8+36*28+24*56
else:
ans=1+14*9+36*36+24*84

#oが0個の時
else:
if s.count("x")==9:
ans=1
elif s.count("x")==8:
ans=1*2+14
elif s.count("x")==7:
ans=1*3+14*3+36
elif s.count("x")==6:
ans=1*4+14*6+36*4+24
elif s.count("x")==5:
ans=1*5+14*4+36*6+24*4
elif s.count("x")==4:
ans=1*6+14*5+36*10+24*10
elif s.count("x")==3:
ans=1*7+14*6+36*15+24*20
elif s.count("x")==2:
ans=1*8+14*7+36*21+24*35
elif s.count("x")==1:
ans=1*9+14*8+36*28+24*56
else:
ans=1*10+14*9+36*36+24*84

print(ans)


<解き方2>

コンテストが終わってから、Pythonでの組み合わせの計算方法を調べる。組み合わせの総数を返す   scipy.special.comb()  が良さそう。

from scipy.special import comb
s=input()

o=s.count("o")
x=s.count("x")

#0になる時
if o>4:
ans=0

#oが4個の時
elif o==4:
ans=24

#oが3個の時
elif o==3:
ans=36+24*(7-x)

#oが2個の時
elif o==2:
ans=14+36*(8-x)+24*comb(8-x,2)

#oが1個の時
elif o==1:
ans=1+14*(9-x)+36*comb(9-x,2)+24*comb(9-x,3)

#oが0個の時
else:
ans=1*(10-x)+14*comb(10-x,2)+36*comb(10-x,3)+24*(10-x,4)


print(ans)

画像2

あれー?さっきより間違ってる…。


<解説>

flag = [False]*10

これは、flagという名のリストを作っているんですね。

print(flag)

<出力>

[False, False, False, False, False, False, False, False, False, False]

nowは0000~9999までの暗証番号候補で、仮にnowを4541とすると、now%10は1。つまり1桁目の数字。flag[now%10] = Trueはflag[1]=Trueになる。now //= 10でnowは10で割った商、454になる。…と繰り返して、
flag=[False, True, False, False, True, True, False, False, False, False] となる。

入力値Sとflagの比較をする。falg2=Trueを立てて、Falseになる場合は、
・S[j] == 'o' かつ flag[j]==False
・S[j] == 'x' かつ flag[j]==True

Falseは0, Trueは1なので、ans += flag2


この記事が気に入ったらサポートをしてみませんか?