• <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 閱讀(196) 評論(0)  編輯 收藏 引用 所屬分類: 數論

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

            導航

            統計

            公告

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            me

            好友

            同學

            網友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            91麻豆精品国产91久久久久久| 久久99精品国产麻豆不卡| 久久成人小视频| 亚洲AV无码久久精品蜜桃| 国产成人精品免费久久久久| 亚洲精品高清国产一久久| 国产高潮国产高潮久久久91| 色综合久久夜色精品国产| 亚洲国产精品无码久久久不卡| 99国产欧美久久久精品蜜芽| 久久成人18免费网站| 亚洲国产精品无码久久久蜜芽 | 久久久久国产精品| 午夜天堂精品久久久久| 99久久精品费精品国产| 久久经典免费视频| 久久免费国产精品| 青青草国产精品久久久久| 婷婷伊人久久大香线蕉AV| 日韩美女18网站久久精品| 7国产欧美日韩综合天堂中文久久久久| 色偷偷91久久综合噜噜噜噜| 狠狠色伊人久久精品综合网 | 久久这里的只有是精品23| 麻豆精品久久精品色综合| 久久久久人妻一区精品性色av| 久久人人爽人人爽人人片AV东京热| 亚洲精品国产成人99久久| 99久久精品日本一区二区免费| 色88久久久久高潮综合影院| 思思久久好好热精品国产| 午夜精品久久久久| 思思久久99热只有频精品66| 久久精品综合网| 久久人人爽人人人人爽AV| 国内精品九九久久精品| 久久亚洲私人国产精品vA| 久久精品中文无码资源站 | 99久久99这里只有免费费精品| 亚洲va国产va天堂va久久| 无码人妻久久一区二区三区蜜桃 |