Recently, in the multi-school puzzle, it is often seen that the giant's handwriting function takes a modulo operation, which seems to be faster than the% operation.
The implementation of the% operation in the computer relies on division, which is obviously not as good as the addition and subtraction of the handwriting function.
You can write a pitch when you are making multiple additions and updates to the module. However, when the algorithm is more complex than O (n), this optimization is of little significance.
1#include <cstdio>2 3 Const intMAXN = 5e8+ One;4 Const intMOD = 1e9+7;5typedefLong LongLL;6 7InlinevoidUpdate (LL &x,ll D)8 {9X + =D;Ten if(x >= MOD) x-=MOD; One if(X <0) x + =MOD; A } - - intMain () the { -LL ans =0; -printf"update () \ n"); - for(intI=0; i<maxn;i++) + { - //ans + = i; + //ans%= MOD; A Update (ans,i); at } -printf"%i64d\n", ans); -}
Looks like a lot of optimization.
CPU is I5-4300U
On the time difference between direct and handwritten modulus functions