Suppose Given a expression = (a*b*c)/(x*y*z) But as the value can be very big you have to output the final value mod 100000000000031 (1 <= a, b, c, x, y, z <= 10^18)
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Suppose Given a expression = (a*b*c)/(x*y*z) But as the value can be very big you have to output the final value mod 100000000000031 (1 <= a, b, c, x, y, z <= 10^18)
Name |
---|
Multiplication is simple. Exponentiation using binary exponantion(works in log n time). Find modular inverse of the divisor with exponantion(fermat's little theorem) and multiply with x.
Bruh moment, we can simply observe $$$a,b,c,x,y,z$$$ after modulo might have maximum value $$$mod-1 = 100000000000030$$$, this is around $$$10^{14}$$$. $$$10^{14}$$$ $$$*$$$ $$$10^{14}$$$ in the $$$mult$$$ function just explodes at $$$10^{28}$$$.
no u
If you don't have
__int128
, you can avoid overflow when multiplying long long by something similar to binpowBut it will take $$$O(\log b)$$$
Since $$$10^{14} < 2^{57}$$$, you can use the double hack from this blog:
You can obviously create your own BigNum structure or something but that is too much work.
Instead we can shift our look from $$$\mathbb{Z}_{M}$$$, where $$$M=10^{14}$$$, to $$$\mathbb{Z}_{\sqrt{ M }}[\sqrt{M}]$$$. We will essentially keep two parts of each number. Maintaining elements of $$$\mathbb{Z}_{\sqrt{ M }}[\sqrt{ M }]$$$ is easy since we only keep two coefficients which are less than $$$\sqrt{ M }=10^7$$$.
Specifically, if $$$x\in \mathbb{Z}_{\sqrt{ M }}[\sqrt{ M }]$$$ then there are $$$a,b\in\mathbb{Z}_{\sqrt{ M }}$$$ such that $$$x=a+b\sqrt{ M }$$$, this will represent the literal $$$a+b\sqrt{ M }$$$ number in $$$\mathbb{Z}_{M}$$$. If $$$y\in\mathbb{Z}_{M}$$$ then with division there are $$$q,r$$$ such that $$$y=q\sqrt{ M }+r$$$ with $$$0\leq r<\sqrt{ M }$$$ and $$$0\leq q<\sqrt{ M }$$$ (else we would have $$$y\geq q\sqrt{M}\geq M>y$$$), so this is the representation of $$$y$$$ in $$$\mathbb{Z}_{\sqrt{ M }}[\sqrt{ M }]$$$.
Now we carry out the multiplication in $$$\mathbb{Z}_{\sqrt{ M }}[\sqrt{ M }]$$$ as
,which we can do now since the multiplications fit in
long long
.Keep in mind that this technique is simple and perhaps nice, but it is not a great solution for multiplying big numbers in general.
i have seen this trick once, here's code
there's no any annoying float number so it's 100% safe.