• <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 若余 閱讀(1588) 評論(0)  編輯 收藏 引用

            導航

            <2011年4月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            統計

            常用鏈接

            留言簿

            隨筆檔案(16)

            搜索

            最新隨筆

            最新評論

            評論排行榜

            91精品国产高清91久久久久久| 欧美综合天天夜夜久久| 亚洲av成人无码久久精品| 亚洲精品乱码久久久久66| 久久精品国产亚洲av水果派| 91精品国产91久久久久久青草| 99久久精品免费看国产一区二区三区 | 亚洲AV日韩精品久久久久久| 亚洲国产精品一区二区三区久久| 伊人久久大香线蕉亚洲| 国内精品人妻无码久久久影院导航 | 久久免费的精品国产V∧| 内射无码专区久久亚洲| 九九久久99综合一区二区| 亚洲人成网站999久久久综合 | 久久久久久国产a免费观看不卡| 亚洲级αV无码毛片久久精品| 久久久精品久久久久久| 久久精品麻豆日日躁夜夜躁| 亚洲欧美一区二区三区久久| 久久本道久久综合伊人| 国产精品久久久福利| 色婷婷久久综合中文久久蜜桃av| 久久精品国产福利国产琪琪| 久久综合欧美成人| 久久精品国产99久久久| 久久乐国产综合亚洲精品| 午夜精品久久久久成人| 久久天天躁狠狠躁夜夜av浪潮 | 成人精品一区二区久久久| 久久中文娱乐网| 国产精品欧美久久久久天天影视| 久久精品视频免费| 久久这里只精品国产99热 | 久久亚洲美女精品国产精品| 久久99国产精品久久| 99国产精品久久久久久久成人热| 久久精品中文字幕一区| 久久99精品国产麻豆| www亚洲欲色成人久久精品| 久久综合九色综合久99|