• <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>
            posts - 43,  comments - 9,  trackbacks - 0
            http://acm.hdu.edu.cn/showproblem.php?pid=1005
            A number sequence is defined as follows:
            f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
            Given A, B, and n, you are to calculate the value of f(n).
            1 <= A, B <= 1000, 1 <= n <= 100,000,000

            解:
            f(n) = (A * f(n - 1) + B * f(n - 2)) %7
                  = (A * f(n - 1) %7 + B * f(n - 2) %7) %7
            所以對于給定的A和B,可以先打表,找出數列的循環部分. 鴿巢原理知,狀態總數不會超過7*7
            注意循環節不一定從f(3)開始...

             1#include <iostream>
             2using namespace std;
             3
             4int a,b,n,x,y,l,h,m[7][7],q[300];
             5int main(){
             6    while(scanf("%d%d%d",&a,&b,&n)!=EOF && (a||b||n)){
             7        memset(m, 0sizeof(m));
             8        q[1= q[2= 1;
             9        x = y = 1; l=3;
            10        while(m[x][y]==0)//該狀態還未經歷過,則擴展
            11            q[l] = (a*x+b*y)%7;
            12            m[x][y] = l;
            13            y = x;
            14            x = q[l++];
            15        }

            16        //此時,q[1h-1]為前面的非循環部分
            17         //q[hl-1]為循環節
            18        h = m[x][y]; //循環節的起始位置
            19        if(n<h) printf("%d\n",q[n]);
            20        else printf("%d\n",q[((n-h)%(l-h))+h]);
            21    }

            22    return 0;
            23}

            posted on 2009-04-25 11:55 wolf5x 閱讀(909) 評論(0)  編輯 收藏 引用 所屬分類: acm_icpc
            <2009年10月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            "Do not spend all your time on training or studying - this way you will probably become very exhausted and unwilling to compete more. Whatever you do - have fun. Once you find programming is no fun anymore – drop it. Play soccer, find a girlfriend, study something not related to programming, just live a life - programming contests are only programming contests, and nothing more. Don't let them become your life - for your life is much more interesting and colorful." -- Petr

            留言簿(3)

            隨筆分類(59)

            隨筆檔案(43)

            cows

            搜索

            •  

            最新評論

            評論排行榜

            色偷偷88欧美精品久久久| 2020久久精品国产免费| 国产一区二区精品久久岳| A级毛片无码久久精品免费| 国内精品久久久久久不卡影院| 久久婷婷人人澡人人| 麻豆成人久久精品二区三区免费 | 久久综合亚洲色HEZYO社区| 亚洲国产成人久久精品99| 精品久久久久久国产| 久久精品国产亚洲一区二区| 欧美久久一区二区三区| 久久亚洲春色中文字幕久久久| 久久精品国产亚洲沈樵| 国产精品亚洲综合久久| 日本福利片国产午夜久久| 一本综合久久国产二区| 欧美亚洲另类久久综合| 欧美精品九九99久久在观看| …久久精品99久久香蕉国产| 久久久久久精品成人免费图片| 欧美一区二区精品久久| 亚洲AV无码久久精品色欲| 91精品免费久久久久久久久| 99久久精品免费看国产一区二区三区| 亚洲国产精品一区二区久久| 日日噜噜夜夜狠狠久久丁香五月| 精品综合久久久久久88小说| 国内精品伊人久久久久AV影院| 久久久人妻精品无码一区| 久久99国产精品一区二区| 亚洲熟妇无码另类久久久| 久久影院午夜理论片无码| 超级碰久久免费公开视频| 精品久久久久久国产| 69国产成人综合久久精品| 亚洲αv久久久噜噜噜噜噜| 中文字幕精品久久久久人妻| 综合久久精品色| 国内精品久久久久影院老司| 久久久久综合中文字幕|