码迷,mamicode.com
首页 > 其他好文 > 详细

快速乘?

时间:2018-10-10 23:52:17      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:space   while   效果   clu   include   open   没有   pen   快速   

快速乘测试对比程序:

技术分享图片
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll rd()
{
    return rand()|(ll(rand())<<32);
}
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;
    return t<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;
    return t<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;
    return t<0?t+md:t;
}
ll mul0(ll x,ll y)
{
    return __int128(x)*y%md;
}
ll a,b;
int main()
{
    int T=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);//可将mul1改为mul2/mul3
        //cout<<t1<<‘ ‘<<t2<<‘\n‘;
        if(t1!=t2)
        {
            printf("%d\n",T);
            puts("test");
            int t;cin>>t;
        }
        //int t;cin>>t;
    }
    return 0;
}
View Code

经过一些测试,可以发现,mul3效果最差(在模数>=1e17时,100000组以内就拍出锅);应该是1e-8不够

mul2效果没有mul1好(模数不设额外上限时,100000组以内出锅;上限1e18时,20秒不出锅)

mul1效果最好(模数不设额外上限时,20秒不出锅)

原因就不知道了。。。

 

快速乘?

标签:space   while   效果   clu   include   open   没有   pen   快速   

原文地址:https://www.cnblogs.com/hehe54321/p/9769531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!