[Question]
  [
## Background
A ray of light is fired from the top left vertex of an `MxN` Chamber, where `M` a denotes the width and `N` denotes the height of the chamber. The ray of light advances one grid space per second. Given that `T` is the number of seconds to be simulated, calculate the number of reflections in this time frame.
For example, given `5 4 11` (ie. `M = 5, N = 4, T = 11`):
```
\/\  [
/\ \ [
\ \ \[
   \/[
-----
There would be 4 reflections, so the output should be 4.
```
Note that a reflection only counts if the ray of light has already bounced off it, so for example, given `5 4 10`:
```
\/\  [
/\ \ [
  \ \[
   \/[
-----
There would only be 3 reflections, so the output should be 3.
```
## Your Task
* Sample Input: M, the width of the chamber, N, the height of the chamber, and T, the time frame. These are all numbers.
* Output: Return the number of reflections.
**Explained Examples**
```
Input => Output
1 1 10 => 9 
Chamber:
\[
-
The ray will be reflected back and forth a total of 9 times. 
```
```
Input => Output
5 1 10 => 9 
Chamber:
\/\/\[
-----
The ray will be reflected back and forth a total of 9 times. It will first go left to right, then go backwards right to left.
```
```
Input => Output
4 5 16 => 6 
Chamber:
\/\ [
/\ \[
\ \/[
 \/\[
\/\/[
----
The ray will be reflected back and forth a total of 6 times.
```
```
Input => Output
100 100 1 => 0 
Chamber:
\ ... [
...    x100
-x100
The ray never touches a wall, and is never reflected, so output 0.
```
**Test Cases**
```
Input => Output
5 4 11 => 4
5 4 10 => 3
1 1 10 => 9
5 1 10 => 9
4 5 16 => 6
100 100 1 => 0
3 2 9 => 5
5 7 5 => 0
3 2 10 => 6
6 3 18 => 5
5 3 16 => 7
1 1 100 => 99
4 4 100 => 24
2398 2308 4 => 0
10000 500 501 => 1
500 10000 502 => 1
```
Bonus points (not really): Listen to DeMarco's song [Chamber of Reflection](https://www.youtube.com/watch?v=pQsF3pzOc54) while solving this.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answer wins.
  
[Answer]
# [Python](https://www.python.org), 47 bytes
```
lambda M,N,T:len({*range(M,T,M),*range(N,T,N)})
```
[Attempt This Online!](https://ato.pxeger.com/run?1=RY9NTsMwEIX3PsWoK7sy4J84JJXCCZquumRjRFMspY4VwgIhTsKmEoLbcIDehvEPYjHSe9943pM_vsPr8jT58-fQ3X-9LMNVc7kZ7enh0ULPd3y_GQ-evq1n648H2vM97xkvDrd8x95ZObPDNMMWnIcp4I1gGwKOT9DB9vo5jG6hK-juYMUQI3QFol3nZycbqPMLp9Ez5GFGS9HwARkrRefLj4EKpIxpFUlaRK2JBFl0i_xfV4CujromUghIE60gGhS0UZok80VNatAgm8xN1On6tjTk2Jhb_TlVEaXbBpQWDdKUjStcmjSpThKT2xNSGeVf_QI)
### How?
Enumerates and counts all bouncing times using the Python set type for deduplication.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 58 bytes
```
g(a,b){a=b?g(b,a%b):a;}f(M,N,T){T=--T/M+T/N-T*g(M,N)/M/N;}
```
[Try it online!](https://tio.run/##VVBBboMwEDzjV6xSRbLDUiCEKKlL@gI4@db2YCBQpBYQgV4Qb6d2IAm9jHZm1jveTaw8ScYxpxJj1ssgfstpjHIdsxfJh4yGGKFgvQgsS9ihKezIEptcy8wO7YgP41NRJt9deobXS5sW1fPXiZCibMmPLEr6WxUpIz0xLm3TJS2oylAmhAgRguDEGEC@f0JwdXofYYfgugMumDMxV5UP5v9jqk0L@7nTcbSlYRI8hC3CcUluL/cISnAP96neYs6c8QjZLejWOx7UKM9RuLsH61R/gtsW828mcTuJzqA2R9jUnBAjqxqgtbqB5FBbp5CDadZMtdWNulVGV@v0o1whZFS7qHuiKwrG9Pvm3HZNCQ4nw/gH "C (gcc) – Try It Online")
Uses the formula:
\$ \lfloor \frac{T - 1}{M} \rfloor + \lfloor \frac{T - 1}{N} \rfloor - \lfloor \frac{T - 1}{lcm(M,N)} \rfloor\$
[Answer]
# [Nekomata](https://github.com/AlephAlpha/Nekomata), 5 bytes
```
ᵒ÷u#←
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70iLzU7PzexJHFZtJJurlLsgqWlJWm6Fqsebp10eHup8qO2CUuKk5KLocILbs6NNtUxiVUwNOSCMgy4og11DCEMUxjDRMcUyDADShkY6AAxkMMVbaxjFKtgCaVBqsx0jIEMC5A-Y6hyiAFgE0ygLCNjSwsdI2MDi1gFE7CBQCNNQUaaGoAcAbEAwjeCuBIA)
Takes input as `[m,n],t`.
```
ᵒ÷u#←
ᵒ÷      Generate a division table of [0,...,t-1] and [m,n]
  u     Uniquify
   #    Length
    ←   Decrement
```
[Answer]
# [Python 3](https://docs.python.org/3/), 50 bytes
```
lambda x,y,t:len({(i//x,i//y)for i in range(t)})-1
```
[Try it online!](https://tio.run/##VU/LasMwELzrK7YnS@Amll9NAg700i/oLc3BieVW4MhGUiHG@NtdrZTWdEFidnZmVhpG@9WrbGmrj6Wrb5emhns8xvbQCUUnKrfbe@yukbW9BglSga7Vp6CWzeyZL/I29NqCGQ251A1U8FZ3RhAUd1IJ1LvZxthGqgMBV9qJcLQxQyctjapjxPxAtoA7NYOqgjSIsfxznOlWD1QqG4M@JeeHm7E/We8kbkz1iZ9X9urYlvqIlXSbrvBUQb8uwRo0@tuopdN9jmEa8bKzf890ndEx9XPE/pnCr9/1tyAuVvUWmZAb8qLXrgMrjDUw1MaI5iliSwE5cA7VEXLicYI4Ixz4A@8dv@IcXFciLglPEvAH24SQDFLYIy48DJaSlJAB3wW@QOztL48VIReD898uzUma7XeQZsnOsT7cjdyw8Mfv46QI6z2VeuoH "Python 3 – Try It Online")
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), ~~49~~ 48 bytes
```
f=lambda M,N,T:(T:=T-1)and(T%M*(T%N)<1)+f(M,N,T)
```
[Try it online!](https://tio.run/##RVBBaoRAELzPKwoh6CRucBw1KjGQB@jJ28aDQSWCcUU9bF5vplt3d2Ckurqq2unpb/25jDqe5m3rsqH@/W5q5G7hlqlTpll5UrIeG6d8yp/Np5DvSr50Dgvk1l1mDP3Yoh9hWVaIAEoh@0AgGHuEtVBQB04M/8ABTBURjoTyPPCl0hNCw0dCOGS4WyIRQUPFOx8SZvvbMWLPpeDgVvmB8HUSw9debFgONy3TDPnyPCXCfTxTPlPmPa/LNPSrY3@NtkwFzOk7fu9e0GHFgozpQy/v3dxF4aI07XM/rs5VgjZ2pXXtxnOqq@oun1tKIuXRPanqEVYvSzuvuC0fWUYGMc1ksD@HAWu7mH@ZSNjYcvsH "Python 3.8 (pre-release) – Try It Online")
-1 byte thanks to c--
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ḍƇⱮṖTL
```
A dyadic Link that accepts the dimensions on the left and the time on the right and yields the number of completed reflections.
**[Try it online!](https://tio.run/##y0rNyan8///hjt5j7Y82rnu4c1qIz/@jkx7unKFzeLl@1qOGOQq2dgqPGuZqRv7/Hx0dbaqjYBKro2BoGKvDpYDgGkC4hkAmEtcUlWuio2AK4ppBFRsYgOQMYsGqQCLGOgpGQJ4lCg@m20xHwRjEtYCbbYxsGMwqhF0myHwjY0sLoHnGBkDtIBmoA0BOMAU7wdQA5iWos6CiQCdwxQIA "Jelly – Try It Online")**
### How?
```
ḍƇⱮṖTL - Link: dimensions = [M, N, ...]; time = T
   Ṗ   - pop {T} -> Seconds=[1,2,3,...,T-1]
  Ɱ    - map {across S in Seconds} with:
 Ƈ     -   keep {dimensions} that:
ḍ      -     divide {S}
    T  - truthy indices
     L - length
```
---
Lots more sixes such as, [TIO](https://tio.run/##y0rNyan8///hzmmH21zSjrX7/D866eHOGTqHlzvoZz1qmKNga6fwqGGuZuT//9HR0aY6CiaxOgqGhrE6XAoIrgGEawhkInFNUbkmOgqmIK4ZVLGBAUjOIBasCiRirKNgBORZovBgus10FIxBXAu42cbIhsGsQthlgsw3Mra0AJpnbADUDpKBOgDkBFOwE0wNYF6COgsqCnQCVywA):
```
ṖÆDfƇL - Link: time = T; dimensions = [M, N, ...]
Ṗ      - pop {T}
 ÆD    - divisors
    Ƈ  - keep those for which:
   f   -   filter keep {dimensions}
     L - length
```
`RmFQL’` [TIO](https://tio.run/##y0rNyan8/z8o1y3Q51HDzP9HJz3cOUPn8HIH/axHDXMUbO0UHjXM1Yz8/z86OtpUR8EkVkfB0DBWh0sBwTWAcA2BTCSuKSrXREfBFMQ1gyo2MADJGcSCVYFEjHUUjIA8SxQeTLeZjoIxiGsBN9sY2TCYVQi7TJD5RsaWFkDzjA2A2kEyUAeAnGAKdoKpAcxLUGdBRYFO4IoFAA)
`ḍẸ¥ⱮṖS` [TIO](https://tio.run/##y0rNyan8///hjt6Hu3YcWvpo47qHO6cF/z866eHOGTqHl@tnPWqYo2Brp/CoYa5m5P//0dHRpjoKJrE6CoaGsTpcCgiuAYRrCGQicU1RuSY6CqYgrhlUsYEBSM4gFqwKJGKso2AE5Fmi8GC6zXQUjEFcC7jZxsiGwaxC2GWCzDcytrQAmmdsANQOkoE6AOQEU7ATTA1gXoI6CyoKdAJXLAA)
`×ⱮFQ<S`, `×þFQ<S`, `ḍⱮṖṀƇL`, `ḍþṖṀƇL`, `ḍⱮṖṀ€S`, `ḍþṖṀ€S`, `ḍⱮṖẸ€S`, `ḍþṖẸ€S`, `ḍⱮṖẸƇL`, `ḍþṖẸƇL`, `Ḷ:€QL’`, `:þSṖṬS`, ...
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 36 bytes
*port of [user1609012's Python answer](https://codegolf.stackexchange.com/a/262876/112488)*
```
f=(M,N,T)=>--T&&!(T%M&&T%N)+f(M,N,T)
```
[Try it online!](https://tio.run/##TZDNbsIwEITvfortIZEtO@DfNFEV3gBOOSIVxySIKkoQVLx@8E@qcljrmxntjuQf@7QPd7/efotpPvfLMjR4zw6sJc2uKNo8/8Btts/zNjsQOqzR8nVCBjQIAc0OdGIeWCEBYuXa@/@swasycIkE5xAnSI6QAgl1YBMxrZSoBAWiSr4JHNc/14p0NxzWf0pqJFVdgVS88m487iMfmjixTyCT6qMlk3Xa3PvbaF2Pt/h4pgTe3u2F4W9mWcec/xQ3T4957DfjfMEDppbRjlFHCFle "JavaScript (Node.js) – Try It Online")
## Explanation
Start from the end of the ray \$(t = T)\$, and count the number of times it bounced in it's way, that is, anytime \$t \equiv 1 \pmod M \lor t \equiv 1 \pmod N\$. It's easier to see if you extend the chamber infinitely and have the ray cross the chamber walls.
---
# [C (gcc)](https://gcc.gnu.org/), 39 bytes
*port to C*
```
f(M,N,T){T=--T?!(T%M&&T%N)+f(M,N,T):0;}
```
[Try it online!](https://tio.run/##VVDLboMwEDzjr9ikIrLLUplXlNRN@gVw8q3NAUFokVpABHpBfDu1AyTkMtqZWe94N7G/kmQYMhpihJJ18mDb8n1FpRluNtKMmDVbr1z0w1NeJD9teoa3S5Pm5cv3kZC8aMhvnBf0r8xTRjpiXJq6TRpQlaFMCBEiBCmI0UP8cYLD1ekCBB/BcXpcMD4yR5V3Fjww1aaF7dTJubY0jIKH4CLsl2R@uUVQgrO7TfUWc6aMe4i/oK6336lRHlfo34J1ajDCvMX0m1F0R5H3anOE50oQYmRlDbRSN4gFVPYxFGBZFVNtVa1uldG1mX4Wa4SMahd1T3RFyZh@X5@bti6AC9IP/w "C (gcc) – Try It Online")
[Answer]
# [J](https://www.jsoftware.com), 18 bytes
```
<:@#@~.@(<.@%/~i.)
```
Uses the division table method used by others. Expects `M N f T`.
[Attempt This Online!](https://ato.pxeger.com/run?1=m70wa8FOJT31NAVbKwV1BR0FAwUrINbVU3AO8nFbWlqSpmuxycbKQdmhTs9Bw0bPQVW_LlNPEyJx86MmV2pyRr6CqYKJQpqCoSEKzwDCMwRCBM8UhWcC5AN5ZlCVBgZgDBQBC6irQ8SNFYyAYpbIHJgBZkA-kGcBM9wY2TioVXC7TJC5RsaWFkDCwAIoaAK3H2i7KdgFpgYwz0AcBRU0gnh8wQIIDQA)
```
<:@#@~.@(<.@%/~i.)­⁡​‎‎⁪⁡⁪⁠⁪⁣⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁢⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁤⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁡⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁤⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏‏​⁡⁠⁡‌­
        (        )  NB. ‎⁡Dyadic hook
               i.   NB. ‎⁢0..y-1
             /~     NB. ‎⁣Table with flipped arguments, execute a u b for every a in x and b in y
         <.@%       NB. ‎⁤Divide then floor
<:@#@~.@            NB. ‎⁢⁡Then uniquify, length, and decrement
```
# [J](https://www.jsoftware.com), 19 bytes
```
+`-/@(<.@%[,*./)~<:
```
Uses the closed form from [c--'s](https://codegolf.stackexchange.com/a/262883/110888) answer, so please go upvote!
Expects `M N f T`, where `M N` is a list.
The major form is two hooks, an inner and outer hook, `(H F)~ G`, which is equivalent to `(G y) H (F x)`, with `+`-/` tacked on at the end, where `x` and `y` are the left and right arguments, respectively.
[Attempt This Online!](https://ato.pxeger.com/run?1=m70wa8FOJT31NAVbKwV1BR0FAwUrINbVU3AO8nFbWlqSpmuxWTtBV99Bw0bPQTVaR0tPX7POxgoic_OjJldqcka-gqmCiUKagqEhCs8AwjMEQgTPFIVnAuQDeWZQlQYGYAwUAQuoq0PEjRWMgGKWyByYAWZAPpBnATPcGNk4qFVwu0yQuUbGlhZAwsACKGgCtx9ouynYBaYGMM9AHAUVNIJ4fMECCA0A)
```
+`-/@(<.@%[,*./)~<:­⁡​‎⁪⁪⁠⁪⁪⁠‎⁪⁡⁪⁠⁪⁢⁡⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁣⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁡⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁪‏‏​⁡⁠⁡‌⁤​‎⁪⁪⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁪‏‏​⁡⁠⁡‌­
                 <:  NB. ‎⁡Decrement T
     (         )~    NB. ‎⁢Flip the arguments for the inner hook
          [,*./      NB. ‎⁣Append lcm(M,N) to the list of dimensions
      <.@%           NB. ‎⁤Divide T-1 by each item of the result then floor
+`-/@                NB. ‎⁢⁡Then reduce by both + and -, i.e. +`-/ 6 3 1 = 6 + 3 - 1 = 8
```
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 33 bytes
```
@(M,N,T)nnz(union(1:M:T,1:N:T))-1
```
[Try it online!](https://tio.run/##Zc9LDoIwEAbgvSdpk1/S6QNpExIvACsuYFQSNsXEx8LL11IMBV3M5puZzt/x/Di9rqGvi6IIR9agRce9f7OnH0bPyDWuA7nWdZzvKVyG@431zECDiNe15rs1iUhqIQLNZFdTf6QRsYxU5kUhkCqqWFRBwkYxG0mv5dUSClRtpsxE04HDT7KUYx1Ef1Hmb0llK0glKuhNmDgYA5pUU0zK5@bwqSNTJ3wA "Octave – Try It Online")
Thanks to @Luis Mendo for -2.
### [Octave](https://www.gnu.org/software/octave/), 35 bytes
```
@(M,N,T)numel(union(1:M:T,1:N:T))-1
```
[Try it online!](https://tio.run/##Zc9NDoIwEAXgvSdpkyfp9AehCYkXgBUXMAoJiQKJ4vVrKYaCLmbzzUzndbi@Lu/GtUWSJO7MSlSoeT89mjub@m7oGdnS1iBb2ZrzI7lb9xxZyww0iHhRaH7YkvCkViLQQvlm6o80PKae0rgoBEJ5FasqSORezE7Ca3E1hQJluykz03zg9JMs5NgG0V@U8VtS5RmkEhn0Lowf9AFNqDkmxXNL@NCRoeM@ "Octave – Try It Online")
Port of @Albert.Lang's Python answer.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 46 bytes
```
M=>N=>g=(T,x,y)=>T--?!(x*y)+g(T,-~x%M,-~y%N):T
```
[Try it online!](https://tio.run/##TZDLbsIwEEX3/orpAsluHPAzTYqcfgGssqwExoSoVUQQoCrZ9NdTP6jaxYzO3NHcK82n/bI3d/243PPzcGznk5k3pt6aujO4oSOdiKmbPH97wuPzRLLOi/n3uNj4Pi225LWZ13ukQQHnYGpQiVlgiTjwB1de/2MFfioCF4gzBrHCyBCSIKAKrCOmkwIVIIGXSdeB4/nLIyL5BmP1OwmFhKxKEJKVXo3mfuWXOlbM40in@CiJJO2X1/bSW9fiFX4/ZgT@9VVH8Y5aeqDOP8YN59vQt8t@6PAJZ5bg7ODLEULmHw "JavaScript (Node.js) – Try It Online")
[Answer]
# [Scala](https://www.scala-lang.org/), 156 bytes
Golfed version. [Try it online!](https://tio.run/##bZHBboJAEIbvPMVcmszEtdkVNUqLSdNTD3qpPZkeVgqUhmIDm0aCPjudBVKFlmT/yXw7888ARaBTXR/2H2FgYK2TDKr6LYwgwrX3lBnCTRM8ZBXdIX/F6le2LsatLRBHq74UZRupCUmE23tFYwVhWoTA6dH35elUspIace9YCTyOFN2sBZY2bqgp/f/qLj7XAHbsJ2@KOo8LDx7yXJe7Z5MnWfxKHrxkiQEfKgf4@dYpmLAwj7oIC6ZNMTZXADgTMBWgFIkBkReiOO2T2R/CLRbOr7qktCVWLtAVMBGwHIJrp7kAhmrRm@YOvLv5/QWmAzRxlwu2dyXrtLeY3WrWyvWbdxu3FxNqODlN@P2Ct9EhD3XwDhUEnANqAXsBAYG/6oy@@D@YNMMINeGeMBDAjpJaw7Njz9mpfwA)
```
def f(M:Int)(N:Int):(Int,Int,Int)=>Int={def g(T:Int,x:Int=0,y:Int=0):Int=if(T<1)-1 else if(x==0||y==0)1+g(T-1,(x+1)%M,(y+1)%N)else g(T-1,(x+1)%M,(y+1)%N);g}
```
Ungolfed version. [Try it online!](https://tio.run/##jVI9T8MwEN3zK96CdFZdFDdtRSMVCTExtAtlQgxucEJQSFESoUZtf3uwY7dNwsLgO997922Xkcxk0@y2nyqqsJJpjoMHvKsYMa1CPOUVo7XVIUgrjrNgWN4bjWUbY6MS2oTWYx860ueoL3cW9kKANAZtsDQUNmPhUJWVqqX2LYXjEbVzEhiZKhhDcMOPIBhusNJGfTbWrJvnn94nr1WJ565mnC@9EZJFUoZ4KApZvz5XRZonb3qMlzy9zvEjM1SqrB5lqUqNts7kmqAZx5RDCMYHiH9FdHuij8z@IDrEgPNOlO8bFyOuYMAx4VgMgW6mOYcGxV2vWjDI7er3G5gOoEmwuNPpA1/Laa8x09XMiu7krmNLTOzqmV39ZYO38a5QMvrAAZG2QZJjyxGZL@cSfet3qLKcYpKMtowibj6az85vac7Ja5pf)
```
object Main {
  def f(M: Int)(N: Int): (Int, Int, Int) => Int = {
    def g(T: Int, x: Int = 0, y: Int = 0): Int = {
      if (T == 0) T-1
      else if (x == 0 || y == 0) 1 + g(T - 1, (x + 1) % M, (y + 1) % N)
      else g(T - 1, (x + 1) % M, (y + 1) % N)
    }
    g
  }
  def main(args: Array[String]): Unit = {
    val testCases = Array(
      (5, 4, 11),
      (5, 4, 10),
      (1, 1, 10),
      (5, 1, 10),
      (4, 5, 16),
      (100, 100, 1),
      (3, 2, 9),
      (3, 2, 10),
      (6, 3, 18),
      (5, 3, 16),
      (1, 1, 100),
      (4, 4, 100),
      (2398, 2308, 4),
      (10000, 500, 501),
      (500, 10000, 502)
    )
    testCases.foreach { case (a, b, c) =>
      println(f(a)(b)(c, 0, 0))
    }
  }
}
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 15 bytes
```
≔…¹ＮθＩ№×﹪θＮ﹪θＮ⁰
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70iOSOxKDk_MWfBgqWlJWm6FjdLHYuLM9PzNIIS89JTNQx1FDzzCkpL_Epzk1KLNDQ1dRQKNa25Aooy80o0nBOLgUR-KZAZkpmbWqzhm59SmpOvUYipCZcMUMoASFovKU5KLoY6YXm0km5ZjlLsMkMDBVMFE4ggAA) Link is to verbose version of code. Takes `T` as the first parameter. Explanation:
```
 …              Exclusive range from
  ¹             Literal integer `1` to
   Ｎ            First input as a number
≔   θ           Save in variable
         θ      Saved range
        ﹪       Vectorised modulo
          Ｎ     Second input as a number
       ×        Pairwise multiplied by
            θ   Saved range
           ﹪    Vectorised modulo
             Ｎ  Third input as a number
      №         Count of
              ⁰ Literal integer `0`
     Ｉ          Cast to string
                Implicitly print
```
[Answer]
# [Racket](https://racket-lang.org/), 210 bytes
```
(define(r M N T[R 0][X 0][Y 0][x 1][y 1])(let*([A(+ X x)][B(+ Y y)][a(or(< A 0)(> A M))][b(or(< B 0)(> B N))])(if(= T 0)R(r M N(- T 1)(if(or a b)(+ R 1)R)(if a(- X x)A)(if b(- Y y)B)(if a(- x)x)(if b(- y)y)))))
```
[Try it online!](https://tio.run/##dVFNb8IwDL3zKyztYm@q1JSWgdiH6B0OFQdQxSFA2CKqsoWitb@e2Y2EOIRWceP3/PVcp3dH01yfKl1/gfMO7s3B1gYdzGEBy7KAeFOuxKzFtKA2ZceGsDLNM5YzfIEVtLQpc76toeObxpPDN5hBTPjBnzkxuPVg7sEcFgwS2gO@w5KxwnfEiD3V4ycHGrbEVQtGCsFAMy/dZr23ZU865jeupfbGdNSRPFd06Ize072lwWA6hSjidubcwE6fzRnwz1YVnOqqgx9n6wa40kHbiiRwwIm/F@sMSkBk635jl9py9rfZHSNmdfUplfEeABaWQQpKEaT0gIsJhgFOycvcJJj3mEuFHRGMQjXj2B/irQdmHULCNSGjMCUtQ2VHzKpxOC8Tjsd5fShRdISFpJ5NQqtLhpMxm3jMixUxIa2sNOsP61Wh2fw6@pBEQq7@Z/0D "Racket – Try It Online")
---
## Explanation
For starters, this will definitely not win. Looking at the other answers, there seems to be a simpler algorithm to solving this. But I don't want to just copy things without having any understanding on how or why they work, so I decided to use a bruteforcing method of actually running the simulation.
Our function receives our three inputs `M`, `N` and `T`. It also receives a predefined state for the recursive loop.
```
(define (reflect M N T
                 [reflections 0]
                 [x-pos 0] [y-pos 0]
                 [dx 1] [dy 1])
  ...)
```
We then calculate the next `x` and `y` position, and see whether the next position are out-of-bounds (`-oob`).
```
  (let* ([next-x-pos (+ x-pos dx)]
         [next-y-pos (+ y-pos dy)]
         [next-x-oob (or (< next-x-pos 0) (> next-x-pos M))]
         [next-y-oob (or (< next-y-pos 0) (> next-y-pos N))])
    ...)
```
Once those values are calculated, we check whether our simulation time `T` is equal to zero. If it is, we return the resulting number of reflections. Otherwise we repeat the loop with a new set of configurations:
1. Same `M` and `N` sizes.
2. `T` - 1.
3. If the next X and Y positions are out of bounds, `reflections` + 1, else `reflections`.
4. If the next X position is out of bounds, recalculate the next position by flipping `dx`.
5. If the next Y position is out of bounds, recalculate the next position by flipping `dy`.
6. If the previously calculated `next-x-pos` is out of bounds, flip `dx`.
7. If the previously calculated `next-y-pos` is out of bounds, flip `dy`.
```
(define (reflect M N T
                 [reflections 0]
                 [x-pos 0] [y-pos 0]
                 [dx 1] [dy 1])
  (let* ([next-x-pos (+ x-pos dx)]
         [next-y-pos (+ y-pos dy)]
         [next-x-oob (or (< next-x-pos 0) (> next-x-pos M))]
         [next-y-oob (or (< next-y-pos 0) (> next-y-pos N))])
    (if (= T 0)
        R
        (reflect M N (- T 1)
                 (if (or next-x-oob next-y-oob)
                     (+ reflections 1)
                     reflections)
                 (if next-x-oob
                     (- x-pos dx)
                     next-x-pos)
                 (if next-y-oob
                     (- y-pos dy)
                     next-y-pos)
                 (if next-x-oob
                     (- dx)
                     dx)
                 (if next-y-oob
                     (- dy)
                     dy))))
```
*Have an awesome weekend!*
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 8 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
<Lsδ÷Ùg<
```
[Try it online](https://tio.run/##yy9OTMpM/f/fxqf43JbD2w/PTLf5/9/QkCvaVMckFgA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfWXCoZXF/218is9tObz98Mx0m/86/6OjDQ11ok11TGJjdRSiDQ1Q2YY6hkjiULaZTrSJjimEDVRiYKADxBAuklGWOtHGOkZgpilI1BxuEEzY0EIn2kzHGG6oKYxtgGKzAcg6iJkmOtFGxpYWOkbGBhYQkw0gLgAqMoW6wdTACGgUxFEgoVgA).
**Explanation:**
```
<L        # Use the first (implicit) input, and push a list in the range [1,input-1]
  s       # Swap to push the second (implicit) input-pair
   δ      # Apply double-vectorized:
    ÷     #  Integer-division
     Ù    # Uniquify this list of pairs
      g   # Pop and push the length
       <  # Decrease it by 1
          # (after which the result is output implicitly)
```
[Answer]
# [Thunno 2](https://github.com/Thunno/Thunno2) `L`, 6 [bytes](https://github.com/Thunno/Thunno2/blob/main/docs/codepage.md)
```
ėsȷ÷Uṫ
```
[Try it online!](https://Not-Thonnu.github.io/run#aGVhZGVyPSZjb2RlPSVDNCU5N3MlQzglQjclQzMlQjdVJUUxJUI5JUFCJmZvb3Rlcj0maW5wdXQ9MTElMEElNUI1JTJDNCU1RCZmbGFncz1M)
Port of alephalpha's Nekomata answer.
#### Explanation
```
ėsȷ÷Uṫ  # Implicit input
ė       # Push [1..t) to the stack
 s      # Swap so [m,n] is on top
  ȷ     # Outer product over:
   ÷    #  Integer division
    U   # Uniquify the list
     ṫ  # Remove the last item
        # Implicit output of length
```
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 7 bytes
```
tl{/R.Q
```
[Try it online!](https://tio.run/##K6gsyfj/vySnWj9IL/D/f0suYy4jAA "Pyth – Try It Online")
Port of [@alephalpha](https://codegolf.stackexchange.com/a/262899/73054)'s Nekomata answer.
Takes three inputs: time, width, height in that order.
### Explanation
```
tl{/R.QQ    # implicitly add Q
            # implicitly assign Q = eval(input()) and .Q to a list of the remaining input
   /R.QQ    # map division over range(Q) with .Q as the second argument, this automatically vectorizes
  {         # deduplicate
 l          # take the length
t           # subtract 1
```
]