• 快速乘?


    快速乘测试对比程序:

    #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<<'
    ';
            ll t1=mul1(a,b),t2=mul0(a,b);//可将mul1改为mul2/mul3
            //cout<<t1<<' '<<t2<<'
    ';
            if(t1!=t2)
            {
                printf("%d
    ",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秒不出锅)

    原因就不知道了。。。

  • 相关阅读:
    day 03
    day 02
    day 02 作业
    day 01
    day 10 预科
    day 09作业 预科
    day 09 预科
    day 08作业 预科
    The word 'localhost' is not correctly spelled 这个问题怎么解决
    不能够连接到主机(名称为localhost)上的MySQL服务”
  • 原文地址:https://www.cnblogs.com/hehe54321/p/9769531.html
Copyright © 2020-2023  润新知