c++的一個int128類

lt發表於2017-03-13

來源http://tringi.trimcore.cz/int128
上述地址下載的是模板化的版本。更早的版本見這裡這裡
另外一個實現見這裡

用較早版本實現的p474.

#include <cstdio>
#include "int128.h"

typedef long long ll;
const int NN=1000000;
const int N=1000;
int main()
{
    int128 result = 1;
    int128 modB = 10000000000000061LL;
    static int a[NN+1]= {0};

    for(int i=3; i<=N; i++)
        if(a[i]==0)
            for(int j=i*i; j<=NN; j+=2*i)
                if(a[j]==0)
                    a[j]=1;

    for (ll p = 7; p < NN; p+=2)
    {
        if (a[p]==0 && p!=401)
        {
            ll po = p;
            ll total = 0;
            while(true)
            {
                ll add = NN/po;
                if (add == 0) break;
                total += add;
                po*=p;
            }

            int128 mult = total+1;
            result = result*mult%modB;
        }
    }
    result = result*(int128)249997%modB;

    printf("%lld\n",result.toInt64());
}

使用模板版本的474題,速度快於前一個.

#include <cstdio>
#include "int128.hpp"
typedef int128_t int128;
typedef long long ll;
const int NN=1000000;
const int N=1000;
int main()
{
    int128 result = 1;
    int128 modB = 10000000000000061LL;
    static int a[NN+1]= {0};

    for(int i=3; i<=N; i++)
        if(a[i]==0)
            for(int j=i*i; j<=NN; j+=2*i)
                if(a[j]==0)
                    a[j]=1;

    for (ll p = 7; p < NN; p+=2)
    {
        if (a[p]==0 && p!=401)
        {
            ll po = p;
            ll total = 0;
            while(true)
            {
                ll add = NN/po;
                if (add == 0) break;
                total += add;
                po*=p;
            }

            int128 mult = total+1;
            result = result*mult%modB;
        }
    }
    result = result*(int128)249997%modB;

    printf("%s\n",result.c_str(10));
}

相關文章