• <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>
            經(jīng)典題型。如果列數(shù)較少,就能用我們熟知的狀態(tài)壓縮DP解決。但現(xiàn)在列數(shù)有231。考慮到相鄰兩列之間狀態(tài)轉(zhuǎn)移規(guī)則是相同的,我們可以用矩陣表示這種轉(zhuǎn)移規(guī)則,而最后的結(jié)果就是求這個(gè)轉(zhuǎn)移矩陣的n次冪的左上角元素。


            /*************************************************************************
            Author: WHU_GCC
            Created Time: 2007-10-6 11:05:55
            File Name: pku3420.cpp
            Description: 
            ***********************************************************************
            */

            #include 
            <iostream>
            using namespace std;
            #define out(x) (cout<<#x<<": "<<x<<endl)
            const int maxint=0x7FFFFFFF;
            typedef 
            long long int64;
            const int64 maxint64 = 0x7FFFFFFFFFFFFFFFLL;
            template
            <class T>void show(T a, int n){for(int i=0; i<n; ++i) cout<<a[i]<<' '; cout<<endl;}
            template
            <class T>void show(T a, int r, int l){for(int i=0; i<r; ++i)show(a[i],l);cout<<endl;}

            int g[16][16];
            int M;

            void dfs(int old_state, int now, int new_state)
            {
                
            if (now > 3)
                
            {
                    g[old_state][new_state]
            ++;
                    
            return;
                }

                
            if (old_state & (1 << now))
                    dfs(old_state, now 
            + 1, new_state);
                
            if (now <= 2)
                
            {
                    
            if ((old_state & (1 << now)) == 0 && (old_state & (1 << (now + 1))) == 0)
                        dfs(old_state, now 
            + 2, new_state);
                }

                
            if ((old_state & (1 << now)) == 0)
                    dfs(old_state, now 
            + 1, new_state | (1 << now));
            }


            void mul(int a[16][16], int b[16][16], int c[16][16])
            {
                
            for (int i = 0; i < 16; i++)
                    
            for (int j = 0; j < 16; j++)
                    
            {
                        c[i][j] 
            = 0;
                        
            for (int k = 0; k < 16; k++)
                            c[i][j] 
            += a[i][k] * b[k][j];
                        c[i][j] 
            %= M;
                    }

            }


            int calc(int n)
            {
                
            int p[16][16];
                memcpy(p, g, 
            sizeof(p));
                
                
            int ans[16][16], tmp[16][16];
                memset(ans, 
            0sizeof(ans));
                
            for (int i = 0; i < 16; i++)
                    ans[i][i] 
            = 1;

                
            while (n)
                
            {
                    
            if (n & 1)
                    
            {
                        mul(ans, p, tmp);
                        memcpy(ans, tmp, 
            sizeof(tmp));
                    }

                    n 
            >>= 1;
                    mul(p, p, tmp);
                    memcpy(p, tmp, 
            sizeof(tmp));
                }

                
            return ans[0][0];
            }


            int main()
            {
                memset(g, 
            0sizeof(g));
                
            for (int i = 0; i < 16; i++)
                    dfs(i, 
            00);
                
                
            int n;
                
            while (scanf("%d%d"&n, &M), n + M != 0)
                
            {
                    printf(
            "%d\n", calc(n));
                }

                
            return 0;
            }
            posted on 2007-10-08 09:19 Felicia 閱讀(1126) 評論(0)  編輯 收藏 引用 所屬分類: 動(dòng)態(tài)規(guī)劃
             
            狠狠精品干练久久久无码中文字幕| 久久久精品国产免大香伊| 久久精品99久久香蕉国产色戒| 99久久精品免费看国产一区二区三区 | 色婷婷噜噜久久国产精品12p| 色综合久久久久综合体桃花网| 99久久婷婷国产综合亚洲| 久久久久无码精品国产app| 久久人人爽人人爽人人片AV东京热| 久久精品无码一区二区三区日韩| 久久精品中文字幕一区| 99久久无色码中文字幕人妻| 嫩草影院久久99| 97精品伊人久久久大香线蕉| 99久久国语露脸精品国产| 欧美日韩中文字幕久久久不卡| 久久人人爽爽爽人久久久| 日日狠狠久久偷偷色综合免费 | 久久99精品国产99久久6男男| 香蕉99久久国产综合精品宅男自| 久久国产乱子伦精品免费强| 狠狠色狠狠色综合久久| 久久久黄片| 久久99精品久久久久久不卡| 成人妇女免费播放久久久| 久久精品国产亚洲AV蜜臀色欲| 久久综合色区| 91精品国产91热久久久久福利| 久久精品无码专区免费东京热| 久久精品国产乱子伦| 一本大道久久香蕉成人网| 久久综合五月丁香久久激情| 国产AV影片久久久久久| 久久久久久久尹人综合网亚洲| 久久精品www| 久久精品一区二区国产| 亚洲中文字幕久久精品无码喷水 | …久久精品99久久香蕉国产| 久久人人爽人人爽人人片av麻烦 | 色成年激情久久综合| 999久久久免费国产精品播放|