• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj 1811 Prime Test 數論 素數測試

             代碼如下:
            #include <stdio.h>
            #include <time.h>
            #include <math.h>
            #include <stdlib.h>
            #include <algorithm>
            using namespace std;
            typedef unsigned long long LL;
            #define MAX (5000000)
            bool bPrime[MAX];
            void InitPrime()
            {
                int nMax = sqrt((double)MAX) + 1;
                bPrime[0] = bPrime[1] = true;
                for (int i = 2; i <= nMax; ++i)
                {
                    if (!bPrime[i])
                    {
                        for (int j = 2 * i; j < MAX; j += i)
                        {
                            bPrime[j] = true;
                        }
                    }
                }
            }
            LL multAndMod(LL a, LL b, LL n)
            {
                LL tmp = 0;
                while (b)
                {
                    if(b & 1)
                    {
                        tmp = (tmp + a) % n;
                    }
                    a = (a << 1) % n;
                    b >>= 1;
                }
                return tmp;
            }
            //計算a^u%n
            LL ModExp(LL a, LL u, LL n)
            {
                LL d = 1;
                a %= n;
                while (u)
                {
                    if (u & 1)
                    {
                        d = multAndMod(d, a, n);
                    }
                    a = multAndMod(a, a, n);
                    u >>= 1;
                }
                return d % n;
            }
            //判斷nN是不是合數
            bool Witness(LL a, LL nN)
            {
                LL u = nN - 1, t = 0;//將nN-1表示為u*2^t
                while (u % 2 == 0)
                {
                    t++;
                    u >>= 1;
                }
                LL x0 = ModExp(a, u, nN);//x是a^u
                LL x1;
                for (int i = 1; i <= t; ++i)
                {
                    x1 = multAndMod(x0, x0, nN);
                    if (x1 == 1 && x0 != nN - 1 && x0 != 1)
                    {
                        return true;
                    }
                    x0 = x1;
                }
                if (x1 != 1)
                {
                    return true;
                }
                return false;
            }
            //素數測試
            bool MillerRabin(LL nN)
            {
                //if (nN < MAX)return !bPrime[nN];
                const int TIME = 10;
                for (int i = 0; i < TIME; ++i)
                {
                    LL a = rand() % (nN - 1) + 1;
                    if (Witness(a, nN))
                    {
                        return false;
                    }
                }
                return true;
            }
            LL gcd(LL a, LL b)
            {
                if (a < b)swap(a, b);
                while (b)
                {
                    LL t = a;
                    a = b;
                    b = t % b;
                }
                return a;
            }
            //啟發式尋找nN的因子
            LL PollardRho(LL n, LL c)
            {
                LL i = 1, t = 2;
                LL x, y;
                LL ans;
                srand(time(NULL));  
                y = x = rand() % n;
                while(1)
                {
                    i++;
                    x = (multAndMod(x, x, n) + c) % n;
                    ans = gcd(y - x, n);
                    if(ans > 1 && ans < n)
                        return ans;
                    if(x == y)
                        return n;
                    if(t == i)
                    {
                        y = x;
                        t <<= 1;
                    }
                }
            }
            LL FindMin(LL nN, LL c)
            {
                //printf("nN:%I64u\n", nN);
                if (MillerRabin(nN) || nN <= 1)
                {
                    return nN;
                }
                LL p = nN;
                while (p >= nN) p = PollardRho(p, c--);
                if (p > 1)
                    p = FindMin(p, c);//分解p的最小因子
                if (p < nN)
                {
                    LL q = nN / p;
                    q = FindMin(q, c);//找到q的最小因子
                    p = min(p, q);
                }
                return p;
            }
            int main()
            {
                int nTest;
                srand(time(NULL));
                //InitPrime();
                scanf("%d", &nTest);
                while (nTest--)
                {
                    LL nN;
                    scanf("%I64u", &nN);
                    if (nN > 2 && nN % 2 == 0)
                    {
                        printf("2\n");
                    }
                    else if (nN == 2 || MillerRabin(nN))
                    {
                        printf("Prime\n");
                    }
                    else
                    {
                        printf("%I64u\n", FindMin(nN, 181));
                    }
                }
                return 0;
            }

            posted on 2012-09-24 12:56 yx 閱讀(187) 評論(0)  編輯 收藏 引用 所屬分類: 數論

            <2012年10月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            導航

            統計

            公告

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            me

            好友

            同學

            網友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色噜噜狠狠先锋影音久久| 久久人妻少妇嫩草AV蜜桃| 久久这里只精品国产99热| 久久香蕉超碰97国产精品| 国内精品久久久久| 久久91精品国产91久久小草| 久久精品18| 一本久道久久综合狠狠爱| 97久久精品人人澡人人爽| 一级做a爰片久久毛片16| 久久毛片一区二区| 2021国内精品久久久久久影院| 99热成人精品免费久久| 囯产极品美女高潮无套久久久| 久久免费精品视频| 精品国产乱码久久久久久浪潮| 亚洲国产综合久久天堂| 久久这里只有精品视频99| 18岁日韩内射颜射午夜久久成人| 欧美久久亚洲精品| 精品久久久久久无码中文字幕一区| 久久99精品国产麻豆不卡| 午夜精品久久久久久99热| 久久精品国产精品亚洲下载| 久久精品亚洲日本波多野结衣| 人妻系列无码专区久久五月天| 99久久久精品| 亚洲国产精品高清久久久| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 精品免费久久久久久久| 国内精品免费久久影院| 97热久久免费频精品99| 无码国内精品久久人妻| 久久狠狠爱亚洲综合影院| 亚洲伊人久久综合中文成人网| 久久青青草原国产精品免费 | 久久精品国产一区二区三区不卡| 国产成人精品综合久久久| 久久久精品国产亚洲成人满18免费网站| 99久久99久久久精品齐齐| 国产精品99久久精品|