suppose u have to find a % b , i see many red coders solution instead of using directly a%b they do like ..

if (a >=mod) a-=mod ??

why so ?

Before contest

Codeforces Round #680 (Div. 1, based on Moscow Team Olympiad)

18:48:57

Register now »

Codeforces Round #680 (Div. 1, based on Moscow Team Olympiad)

18:48:57

Register now »

*has extra registration

Before contest

Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)

18:48:57

Register now »

Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)

18:48:57

Register now »

*has extra registration

# | User | Rating |
---|---|---|

1 | tourist | 3619 |

2 | Um_nik | 3493 |

3 | ecnerwala | 3446 |

4 | Radewoosh | 3383 |

5 | ksun48 | 3357 |

6 | yosupo | 3324 |

7 | Benq | 3299 |

8 | maroonrk | 3243 |

9 | apiadu | 3238 |

10 | Petr | 3217 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 207 |

2 | Monogon | 196 |

2 | SecondThread | 196 |

4 | vovuh | 188 |

5 | pikmike | 186 |

6 | Um_nik | 185 |

6 | antontrygubO_o | 185 |

8 | Ashishgup | 182 |

9 | pashka | 169 |

10 | Radewoosh | 167 |

suppose u have to find a % b , i see many red coders solution instead of using directly a%b they do like ..

if (a >=mod) a-=mod ??

why so ?

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Oct/31/2020 19:16:04 (h1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Its faster(if you do addition like 1e6(1,000,000) or more times in a program, writing an addition function using this if instead of % will save a lot of time),but this can be used only for addition and a similar if for subtraction. It can not be used for multiplication.

The plausible reason should be that the

execution timeof the statement`if(a >= b) a -= b;`

is more likely to be less than theexecution timeof the statement`a %= b;`

when it is guaranteed that the expression`a < 2*b`

is true. You may write a short C++ program to compare the average execution time of both statements so as to confirm or refute this hypothesis.Yes , i checked in c++14 , its 3.6 times faster than % .. thanks nikolapesic2802 , CodingKnight

With pleasure. Thanks for sharing the result of the experiment.

because

1000 ms

8700 ms