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

            POJ 3318 Matrix Multiplication 隨機化算法

            Matrix Multiplication
            Time Limit: 2000MS Memory Limit: 65536K
            Total Submissions: 11924 Accepted: 2408

            Description

            You are given three n × n matrices A, B and C. Does the equation A × B = C hold true?

            Input

            The first line of input contains a positive integer n (n ≤ 500) followed by the the three matrices A, B and C respectively. Each matrix's description is a block of n × n integers.

            It guarantees that the elements of A and B are less than 100 in absolute value and elements of C are less than 10,000,000 in absolute value.

            Output

            Output "YES" if the equation holds true, otherwise "NO".

            Sample Input

            2
            1 0
            2 3
            5 1
            0 8
            5 1
            10 26
            

            Sample Output

            YES

            Hint

            Multiple inputs will be tested. So O(n3) algorithm will get TLE.

            Source

            給定矩陣A和B,判斷矩陣C是不是它們的乘積。

            題目明確表示直接判斷會超時,而Strass和直接相乘的O(n^3)效果相差不多。
            因而采用隨機化方法,按我自己的想法,隨機測試C中的若干元素,以確定結果,看了討論區,才發現有更加“專業”的辦法。
            隨機生成行向量I,則若A*B=C,那么必有I*A*B=I*C;反之,不一定成立,算法的隨機性正體現在這里。
            用一個必要不充分條件來判斷結果的正確性,比盲目測試效果往往要好得多。
            這個必要條件判斷結果的時間復雜度是O(N^2)的,這是題目輸入數據量可以接受的。
            /*Source Code

            Problem: 3318  User: y09 
            Memory: 3080K  Time: 1063MS 
            Language: C  Result: Accepted 

            Source Code 
            */

            #include 
            <stdio.h>
            #include 
            <time.h>
            #include
            <stdlib.h>
            int main()
            {
                
            int n;
                
            int i,j;
                
            int mat1[500][500];
                
            int mat2[500][500];
                
            int mat3[500][500];
                __int64 te1[
            500]={0};
                __int64 te2[
            500]={0};
                __int64 te3[
            500]={0};
                __int64 te4[
            500]={0};
                time_t t;
                srand((unsigned) time(
            &t));
                scanf(
            "%d",&n);
                
            for(i=0;i<n;i++)
                    
            for(j=0;j<n;j++)
                        scanf(
            "%d",&mat1[i][j]);
                
            for(i=0;i<n;i++)
                    
            for(j=0;j<n;j++)
                        scanf(
            "%d",&mat2[i][j]);
                
            for(i=0;i<n;i++)
                    
            for(j=0;j<n;j++)
                        scanf(
            "%d",&mat3[i][j]);
                
            for(i=0;i<n;i++)
                    te1[i]
            =rand()%100;
                
            for(i=0;i<n;i++)
                    
            for(j=0;j<n;j++)
                        te2[i]
            +=te1[j]*mat1[j][i];
                
            for(i=0;i<n;i++)
                    
            for(j=0;j<n;j++)
                        te3[i]
            +=te2[j]*mat2[j][i];
                
            for(i=0;i<n;i++)
                    
            for(j=0;j<n;j++)
                        te4[i]
            +=te1[j]*mat3[j][i];
                
            for(i=0;i<n;i++)
                    
            if(te3[i]!=te4[i])
                    
            {
                        puts(
            "NO");
                        
            return 0;
                    }

                puts(
            "YES");


                
            return 0;
            }




            posted on 2010-08-27 18:20 若余 閱讀(1578) 評論(0)  編輯 收藏 引用

            導航

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統計

            常用鏈接

            留言簿

            隨筆檔案(16)

            搜索

            最新隨筆

            最新評論

            評論排行榜

            区久久AAA片69亚洲| 久久久久一级精品亚洲国产成人综合AV区 | 久久国产成人| 最新久久免费视频| 少妇久久久久久被弄高潮| 久久久久久综合一区中文字幕| 国产精品一区二区久久精品无码 | 久久午夜福利无码1000合集| 久久久久人妻一区精品色| 精品久久久久久综合日本| 婷婷国产天堂久久综合五月| 久久香蕉国产线看观看99| 国产精品久久久久久久久软件| 国产精品久久成人影院| 久久精品国产第一区二区| 久久久精品人妻一区二区三区蜜桃| 久久国产热精品波多野结衣AV| 亚洲国产成人久久综合碰| 91精品国产高清久久久久久国产嫩草 | 久久精品无码一区二区WWW| jizzjizz国产精品久久| 一本大道久久东京热无码AV| 99久久精品国产一区二区| 国产情侣久久久久aⅴ免费| 亚洲国产精品无码久久久不卡| 精品国产日韩久久亚洲| 久久国产香蕉一区精品| 久久国产精品久久精品国产| 麻豆成人久久精品二区三区免费 | 久久ZYZ资源站无码中文动漫| 精品国产99久久久久久麻豆| 一本一道久久a久久精品综合 | 狠狠色狠狠色综合久久| 国产精品伊人久久伊人电影 | 久久久久久国产精品免费免费| 欧美久久综合性欧美| 亚洲女久久久噜噜噜熟女| 亚洲国产精品无码久久久秋霞2 | 久久久青草久久久青草| 久久青青草原综合伊人| 久久精品9988|