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

            搜索

            最新隨筆

            最新評論

            評論排行榜

            久久久久久亚洲Av无码精品专口 | 99久久精品久久久久久清纯| 久久w5ww成w人免费| 亚洲天堂久久精品| 久久久久久久久久久久久久| 99久久精品国产一区二区| 国产亚洲美女精品久久久久狼| 久久se精品一区二区影院| 2021国产精品久久精品| 国产精品久久久久久一区二区三区 | 亚洲国产高清精品线久久| 日日噜噜夜夜狠狠久久丁香五月| 四虎国产精品免费久久久| 人人狠狠综合久久亚洲高清| 亚洲午夜久久久久久久久电影网 | 久久经典免费视频| 久久99久久99小草精品免视看| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲国产成人久久精品影视| 日本WV一本一道久久香蕉| 久久综合中文字幕| 久久久久成人精品无码中文字幕| 久久久免费观成人影院| 青青国产成人久久91网| 久久丫精品国产亚洲av不卡| 亚洲伊人久久综合中文成人网| 天天久久狠狠色综合| 五月丁香综合激情六月久久| 亚洲精品久久久www| 久久精品成人免费观看97| 97久久精品午夜一区二区| 国内精品久久久久影院薰衣草| 亚洲国产成人精品91久久久| 精品久久久久久无码中文野结衣| 国产欧美久久一区二区| 国产美女久久精品香蕉69| 漂亮人妻被黑人久久精品| 综合人妻久久一区二区精品| 久久久久亚洲AV无码观看| 久久久亚洲AV波多野结衣| 无码人妻久久一区二区三区蜜桃 |