Fast ride?

Source: Internet
Author: User
Tags modulus

Fast Multiply test Comparison procedure:

#include <bits/stdc++.h>using namespaceStd;typedefLong LongLl;ll Rd () {returnRand () | (LL (rand ()) << +);} ll Md;ll Mul1 (ll x,ll y) {x%=md;y%=MD; ll T=x*y-ll ((Long Double) x/md*y+0.5)*MD; returnt<0? t+Md:t;} ll Mul2 (ll X,ll y) {x%=md;y%=MD; ll T=x*y-ll ((Long Double) x*y/md+0.5)*MD; returnt<0? t+Md:t;} ll Mul3 (ll X,ll y) {x%=md;y%=MD; ll T=x*y-ll ((Long Double) x/md*y+1e-8)*MD; returnt<0? t+Md:t;} ll Mul0 (ll X,ll y) {return__int128 (x) *y%MD;} ll A, B;intMain () {intt=0; Srand (3254244);  while(1) {T++; ll a=rd (), b=Rd (); MD=rd ();//%ll (1E18); //cout<<a<< ' <<b<< ' <<md<< ' \ n ';ll T1=mul1 (A, B), T2=mul0 (A, b);//You can change the MUL1 to Mul2/mul3//cout<<t1<< ' <<t2<< ' \ n ';        if(t1!=T2) {printf ("%d\n", T); Puts ("Test"); intT;cin>>T; }        //int t;cin>>t;    }    return 0;}
View Code

After some tests, it can be found that the MUL3 effect is the worst (in the modulus >=1e17, 100000 groups within the pot); it should be 1e-8.

Mul2 effect is not mul1 good (modulus does not set an additional upper limit, 100000 groups within the pot; upper limit 1e18, 20 seconds not out of the pot)

MUL1 the best effect (modulus does not set an additional upper limit, 20 seconds not out of the pot)

The reason is not known ...

Fast ride?

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.