LoginSignup
2
1

More than 3 years have passed since last update.

Atcoder ABC169 A-EをPythonで

Last updated at Posted at 2020-05-31

過去最低のパフォーマンスでした:(

A: Multiplication 1

やるだけ。

ABC169a.py
a,b=map(int,input().split())

print(a*b)

B: Multiplication 2

$N<10^5$なのに普通に回すとTLEになる。が、途中でループから抜けるようにすると通った。オーバーフローした際の演算に時間がかかるのかも(そもそもオーバーフローした時点でWAだが)。

ABC169b.py
n=int(input())
a=list(map(int,input().split()))

ans=1

if 0 in a:
    print(0)
    exit()

for i in a:
   ans*=i
   if ans>10**18:
    print(-1)
    exit()


print(ans)

C: Multiplication 3

解説AC。高精度な計算が必要と感じたら浮動小数点型を扱うときはFloatではなく例えばDecimal使うとかしましょうね、というだけ。知識問題。

ABC169c.py
from decimal import Decimal
from math import floor

a,b=map(Decimal,input().split())

print(floor(a*b))

D: Div Game

$z$を何種類用意できますか?と言い換えられます。
$z$は$N$の素因数の累乗で表せるため、各素因数ごとに同じ素因数の個数を数え、2個以下⇒1つ、3個以上6個未満⇒2つ($p^1$, $p^2$)、6個以上10個未満⇒3つ($p^1$, $p^2$, $p^3$)・・・といったように、各素因数で表現できる$z$の数を数え上げれば良いです。$2^{43}>10^{12}$なので、各素因数の個数については高々42まで考えておけばOK。例外として、1の場合だけは0になります。
素因数分解の関数部分は、こちらを参考にさせていただきました。

ABC169d.py
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

a=factorization(int(input()))

b=[1,3,6,10,15,21,28,36,43]

ans=0
if len(a)==1 and a[0][0]==1:
    print(0)
    exit()

for i in a:
    for j in range(len(b)):
        if i[1]<b[j]:
            ans+=j
            break
print(ans)

E: Count Median

Aの集合の中央値とBの集合の中央値の間が全体の中央値として取りうる値。
Nが偶数の場合は0.5刻みになり、奇数の場合は1刻みになる。

ABC169e.py
def median(q):
    l=len(q)
    q=sorted(q,reverse=True)
    if l%2==0:
        return (q[int(l/2)]+q[int(l/2)-1])/2
    else:
        return q[int((l+1)/2-1)]

n=int(input())

a=[]
b=[]

for i in range(n):
   x,y=map(int,input().split())
   a.append(x)
   b.append(y)

med_a=median(a)
med_b=median(b)

if n%2==0:
    print(int(2*(med_b-med_a)+1))

else:
    print((med_b-med_a)+1)

ちなみに、WA出しまくった八つ当たりではありますが今回のB-Cの問題形式は個人的には好きではないです。同じ頭を使うなら、実装上の制約そのものではなく制約を突破するアルゴリズムについて考えたい・・・今後もこういった問題の出題が続くと、あまり初心者の友達にAtcoder勧められないな、と感じました。

2
1
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1