On the time difference between direct and handwritten modulus functions

Source: Internet
Author: User
Tags modulus

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.