• <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 閱讀(1112) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 動(dòng)態(tài)規(guī)劃
             
            免费一级做a爰片久久毛片潮| 亚洲人成网站999久久久综合| 99久久国语露脸精品国产| 久久夜色tv网站| 久久亚洲国产精品123区| 久久精品人成免费| 久久久久这里只有精品| 久久久久亚洲av无码专区喷水| 99久久免费只有精品国产| 亚洲中文久久精品无码| 婷婷久久精品国产| 久久精品国产只有精品2020| 欧美亚洲国产精品久久久久| 国产精品久久久久一区二区三区 | 国产成人香蕉久久久久| 思思久久99热只有频精品66| 91久久精品国产91性色也| 蜜臀久久99精品久久久久久小说 | 2021国产精品久久精品| 国内精品伊人久久久久网站| 91久久婷婷国产综合精品青草| 精品国产乱码久久久久久人妻| 久久久久亚洲AV无码专区桃色| 9久久9久久精品| 久久综合亚洲欧美成人| 精品综合久久久久久98| 色综合久久夜色精品国产| 色播久久人人爽人人爽人人片aV| 久久免费视频观看| 久久99精品国产麻豆宅宅| 亚洲乱码精品久久久久..| 精品久久久久久国产| 亚洲AV成人无码久久精品老人| 国产69精品久久久久观看软件| 香蕉久久永久视频| 蜜桃麻豆www久久国产精品| 开心久久婷婷综合中文字幕| 女同久久| 久久精品国产男包| 色综合久久无码中文字幕| 婷婷五月深深久久精品|