• <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>

            為生存而奔跑

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 330206
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

            http://acm.pku.edu.cn/JudgeOnline/problem?id=2440
            這是一個遞推的題目,公式的推導過程如下:
            合法的情況:
            000     001     010     011     100     110
            設達到長度n-1的時候,得到的分別以上述串結尾的序列個數分別為
            f1(n-1) f2(n-1) f3(n-1) f4(n-1) f5(n-1) f6(n-1)
            則總共的序列個數F(n-1)=f1(n-1)+f2(n-1)+f3(n-1)+f4(n-1)+f5(n-1)+f6(n-1)
            則:
            f1(n)=f1(n-1)+f5(n-1)
            f2(n)=f1(n-1)+f5(n-1)
            f3(n)=f2(n-1)
            f4(n)=f2(n-1)
            f5(n)=f3(n-1)+f6(n-1)
            f6(n)=f4(n-1)
            F(n)=f1(n)+f2(n)+f3(n)+f4(n)+f5(n)+f6(n)
                =f1(n-1)+f5(n-1)+f1(n-1)+f5(n-1)+f2(n-1)+f2(n-1)+f3(n-1)+f6(n-1)+f4(n-1)
                =F(n-1)+f1(n-1)+f2(n-1)+f5(n-1)
                =F(n-1)+f1(n-2)+f5(n-2)+f1(n-2)+f5(n-2)+f3(n-2)+f6(n-2)
                =F(n-1)+2*f1(n-2)+2*f5(n-2)+f3(n-2)+f6(n-2)
                =F(n-1)+2*f1(n-3)+2*f5(n-3)+2*f3(n-3)+2*f6(n-3)+f2(n-3)+f4(n-3)
                =F(n-1)+F(n-3)+f1(n-3)+f3(n-3)+f5(n-3)+f6(n-3)
                =F(n-1)+F(n-3)+f1(n-4)+f5(n-4)+f2(n-4)+f3(n-4)+f6(n-4)+f4(n-4)
                =F(n-1)+F(n-3)+F(n-4)

            由于是求對2005的模,因此很容易想到肯定有循環出現。經計算循環是200。但是,我下面用矩陣求解。

            根據遞推式構造矩陣的方法是:
            設遞推式是:f(n)  =a1*f(n-1)+a2*f(n-2)+a3*f(n-3)
                        又有:f(n-1)=    f(n-1)
                            和:f(n-2)=                     f(n-2)

             然后就有

                                f(n)         a1   a2   a3     f(n-1)
                               f(n-1)       1    0    0        f(n-2)
                                f(n-2)      0    1    0        f(n-3)

            因此矩陣就是 a1 a2  a3
                                      1    0   0
                                      0    1   0

            對于該題而言,矩陣A是  1   0   1   1
                                                      1   0   0   0
                                                      0   0   0   0
                                                      0   1   0   0
                                                      0   0   1   0
            F(n)=A*F(n-1)
            #include<iostream>
            #include
            <algorithm>
            #include
            <string>
            #include
            <vector>
            #include
            <cmath>
            #include
            <map>
            using namespace std;
            void copy(int sor[][4],int des[][4])
            {
                
            for(int i=0;i<4;i++)
                    
            for(int j=0;j<4;j++)
                        des[i][j]
            =sor[i][j];
            }

            void mul(int sor1[][4],int sor2[][4],int des[][4])
            {
                
            for(int i=0;i<4;i++)
                    
            for(int j=0;j<4;j++)
                    
            {
                        des[i][j]
            =0;
                        
            for(int k=0;k<4;k++)
                        
            {
                            des[i][j]
            =(des[i][j]+sor1[i][k]*sor2[k][j])%2005;
                        }

                    }

            }

            void multiply(int mat[][4],int l,int res[][4])
            {
                
            if(l<=0)
                
            {
                    
            for(int i=0;i<4;i++)
                    
            {
                        
            for(int j=0;j<4;j++)
                            res[i][j]
            =0;
                        res[i][i]
            =1;
                    }

                }

                
            else if(l==1)
                
            {
                    copy(mat,res);
                }

                
            else
                
            {
                    
            int tmp[4][4];
                    multiply(mat,l
            /2,res);
                    mul(res,res,tmp);
                    
            if(l%2)
                        mul(tmp,mat,res); 
            // res = tmp * mat
                    else copy(tmp,res); //tmp copy-> res;
                }

            }

            void multiply2(int res[][4],int b[],int ans[])
            {
                
            for(int i=0;i<4;i++)
                
            {
                    ans[i]
            =0;
                    
            for(int j=0;j<4;j++)
                        ans[i]
            =(ans[i]+res[i][j]*b[j])%2005;
                }

            }

            int solve(int l)
            {
                
            int f[]={1,2,4,6};
                
            if(l<4)
                    
            return f[l];
                
            int mat[4][4]={{1,0,1,1},{1,0,0,0},{0,1,0,0},{0,0,1,0}};
                
            int b[]={6,4,2,1};
                
            int res[4][4],ans[4];
                multiply(mat,l
            -3,res); //res = mat^l
                multiply2(res,b,ans); // ans=res*b
                return ans[0];
            }

            int main()
            {
                
            int l;
                
            while(scanf("%d",&l)!=EOF)
                
            {
                    printf(
            "%d\n",solve(l));
                }

            }
            posted on 2009-08-17 20:47 baby-fly 閱讀(296) 評論(0)  編輯 收藏 引用 所屬分類: Algorithm
            久久精品成人免费观看97| 亚洲精品乱码久久久久久蜜桃不卡| 99久久国产热无码精品免费| 国产精品久久成人影院| 超级碰碰碰碰97久久久久| 日韩精品久久久久久久电影蜜臀 | 久久99精品久久只有精品| 99久久精品国产一区二区蜜芽| 精品国产乱码久久久久软件| 久久久av波多野一区二区| 久久成人国产精品一区二区| 国产午夜免费高清久久影院| 国产免费久久精品99re丫y| 久久99国产精品久久99| 久久精品国产第一区二区三区| 久久人妻少妇嫩草AV蜜桃| 青青草国产精品久久| 久久久精品人妻一区二区三区四| 久久精品这里只有精99品| 久久99精品国产| 丰满少妇高潮惨叫久久久| 午夜欧美精品久久久久久久| 亚洲国产成人久久综合区| 久久国产视屏| 久久中文字幕视频、最近更新| 久久精品国产69国产精品亚洲| 久久久噜噜噜www成人网| 午夜精品久久久久久99热| 午夜精品久久久久久中宇| 亚洲午夜无码久久久久| 狠狠色综合网站久久久久久久高清| 四虎影视久久久免费观看| 三级片免费观看久久| 久久亚洲精品国产亚洲老地址| 欧美午夜A∨大片久久| 综合久久给合久久狠狠狠97色 | 国产成人无码精品久久久久免费| 少妇内射兰兰久久| 久久91精品久久91综合| 99久久免费只有精品国产| 久久影院午夜理论片无码 |