LoginSignup
2
1

More than 3 years have passed since last update.

python の 実数の除算(/)と整数除算(//)

Posted at

問題

問題Aで入力が

1000000000 987654321 123456789

と入力するとき

A, B, C = map(int, input().split())
ans = (A * (A + 1) * B * (B + 1) *  C * (C + 1) / 8) % 998244353
print(int(ans))

のように回答したがWAが出てしまった。
他の人のAC解答を見てみると

A, B, C = map(int, input().split())
ans = (A * (A + 1) * B * (B + 1) *  C * (C + 1) // 8) % 998244353
print(int(ans))

のように/か//の違いしかなかった。

解決

print(type(A * (A + 1) * B * (B + 1) *  C * (C + 1)/8))
print(A * (A + 1) * B * (B + 1) *  C * (C + 1)//8)

としてみると結果はなんと、、、

1.8584458350497822e+51
1858445835049782285757026664950217712384527500000000

というように//が正確に計算している一方で、/は途中で切り捨てていることが分かる。その理由としてはfloat型に変更してしまっていることが挙げられる。

感想

この知識は大事だが、問題をどのように解決するかを思いつくのが遅すぎた。せっかく問題Bを解けたのに、一瞬で解けるような問題Aを落としてしまったのが痛い。

2
1
0

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