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

            為生存而奔跑

               :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團(tuán)隊

            搜索

            •  

            積分與排名

            • 積分 - 330187
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

            http://acm.pku.edu.cn/JudgeOnline/problem?id=2440
            這是一個遞推的題目,公式的推導(dǎo)過程如下:
            合法的情況:
            000     001     010     011     100     110
            設(shè)達(dá)到長度n-1的時候,得到的分別以上述串結(jié)尾的序列個數(shù)分別為
            f1(n-1) f2(n-1) f3(n-1) f4(n-1) f5(n-1) f6(n-1)
            則總共的序列個數(shù)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的模,因此很容易想到肯定有循環(huán)出現(xiàn)。經(jīng)計算循環(huán)是200。但是,我下面用矩陣求解。

            根據(jù)遞推式構(gòu)造矩陣的方法是:
            設(shè)遞推式是: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
            韩国免费A级毛片久久| 亚洲国产精品无码久久久久久曰| 久久久久人妻一区二区三区| 日韩久久久久久中文人妻| 久久99国产综合精品| 欧美大战日韩91综合一区婷婷久久青草| 久久最新免费视频| 韩国无遮挡三级久久| 久久婷婷五月综合成人D啪| 久久免费小视频| 久久久一本精品99久久精品88| 久久九九青青国产精品| 国内精品久久久久影院亚洲| 97久久精品人人澡人人爽| 久久水蜜桃亚洲av无码精品麻豆| 国产亚洲精久久久久久无码AV| 亚洲乱码精品久久久久..| 欧美久久亚洲精品| 99久久精品国产一区二区| 99久久久精品免费观看国产| 国产精品中文久久久久久久| 18岁日韩内射颜射午夜久久成人| 亚洲午夜无码久久久久| 午夜视频久久久久一区 | 久久www免费人成看片| 久久强奷乱码老熟女网站| 欧美精品一区二区精品久久| 久久久久人妻一区二区三区vr| 少妇熟女久久综合网色欲| 伊人久久无码精品中文字幕| 久久无码国产| 久久综合色区| 久久亚洲精品国产精品婷婷 | 久久青草国产手机看片福利盒子| 久久精品夜夜夜夜夜久久| 久久午夜无码鲁丝片| 亚洲精品美女久久久久99| 熟妇人妻久久中文字幕| 精品久久久久香蕉网| 狠狠干狠狠久久| 国产亚洲精午夜久久久久久|