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

            搜索

            最新隨筆

            最新評論

            評論排行榜

            青青草国产97免久久费观看| 漂亮人妻被中出中文字幕久久 | 久久天天躁狠狠躁夜夜不卡| 久久精品国产乱子伦| 香蕉久久AⅤ一区二区三区| 精品国产乱码久久久久久人妻| 婷婷久久久亚洲欧洲日产国码AV| 久久免费大片| 99精品久久久久久久婷婷| 久久影院综合精品| 波多野结衣久久一区二区| 国产人久久人人人人爽| 麻豆亚洲AV永久无码精品久久 | 女人高潮久久久叫人喷水| 999久久久国产精品| 中文字幕无码久久人妻| 模特私拍国产精品久久| 日本一区精品久久久久影院| 亚洲国产精品久久久久| 亚洲精品午夜国产VA久久成人| 91精品观看91久久久久久| 亚洲va久久久噜噜噜久久| 精品无码久久久久久久久久| 精品久久久久久无码中文字幕| 亚洲国产精品无码久久久蜜芽| 久久强奷乱码老熟女网站| 天天爽天天爽天天片a久久网| 色婷婷久久综合中文久久蜜桃av | 国产精品久久久久jk制服| 狠狠色婷婷久久综合频道日韩| 久久强奷乱码老熟女网站| 中文字幕精品久久| 日本精品一区二区久久久| 精品久久久久久国产免费了| 99久久亚洲综合精品网站| 99久久婷婷国产综合精品草原| 久久综合88熟人妻| 日韩乱码人妻无码中文字幕久久| 久久青青草视频| 无码久久精品国产亚洲Av影片 | 久久久久亚洲AV无码专区网站|