Python real division (/) and integer division (//)

problem

Input in Problem A

1000000000 987654321 123456789

When typing

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

I answered like this, but WA came out. Looking at other people's AC answers

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

There was only a difference between / or //.

Solution

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

The result is ...

1.8584458350497822e+51
1858445835049782285757026664950217712384527500000000

You can see that // is calculated accurately, while / is truncated in the middle. The reason is that it has been changed to a float type.

Impressions

This knowledge is important, but it was too late to come up with how to solve the problem. It hurts to drop Problem A, which can be solved in an instant, even though I solved Problem B with much effort.

Recommended Posts

Python real division (/) and integer division (//)
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Batch design and python
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
python input and output
Python and Ruby split
Python> floor division> Truncation division?
Python3, venv and Ansible
Python asyncio and ContextVar
Python hand play (division)
Programming with Python and Tkinter
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Python and ruby slice memo
Python installation and basic grammar
Coordinator and integer linear programming
I compared Java and Python!
Python shallow and deep copy
About Python, len () and randint ()
About Python datetime and timezone
Install Python 3.7 and Django 3.0 (CentOS)
Python environment construction and TensorFlow
Python class variables and instance variables
Ruby and Python syntax ~ branch ~
[Python] Python and security-① What is Python?
Stack and Queue in Python
python metaclass and sqlalchemy declareative
Fibonacci and prime implementations (python)
Python basics: conditions and iterations
Python bitwise operator and OR
Python debug and test module
Python list and tuples and commas
Python variables and object IDs
Python list comprehensions and generators
About Python and regular expressions
python with pyenv and venv
Unittest and CI in Python
Maxout description and implementation (Python)
[python] Get quotient and remainder
Python 3 sorted and comparison functions
[Python] Depth-first search and breadth-first search
Identity and equivalence Python is and ==
Source installation and installation of Python