HDU 4920 Matrix multiplication(std::bitset)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

題目串連 :http://acm.hdu.edu.cn/showproblem.php?pid=4920

題意 :給兩個n*n的矩陣A、B,要求算的A*B (答案對3模數)

(比賽的時候一直想不到怎麼去消複雜度,在最後的時候想到了用三進位壓幾位狀態(就是幾位幾位算)應該可以過的,可是敲完比賽也結束。(壓6位是可以過的)


正解是bitset搞,第一次接觸到bitset這個神器,用起來確實很炫酷。

方法是統計A矩陣mod3後1出現在哪些位置,2出現哪些位置,B也一樣,只是A是以一行為一個“狀態”, 而B是以一列為一個“狀態”,然後&一下,最後.count()統計。

.count()函數似乎速度很快 0.0

 

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <bitset> 6 #include <string> 7  8 using namespace std; 9 const int MAXN = 805;10 const int MOD = 3;11 typedef int Mat[MAXN][MAXN];12 Mat A, B, C;13 bitset<MAXN> A1[MAXN], A2[MAXN], B1[MAXN], B2[MAXN];14 15 int main() {16     int n;17     while (scanf("%d", &n) == 1) {18         for (int i = 1; i <= n; i++) {19             A1[i].reset(); B1[i].reset();20             A2[i].reset(); B2[i].reset();21         }22         for (int i = 1; i <= n; i++) {23             for (int j = 1; j <= n; j++) {24                 scanf("%d", &A[i][j]);25                 A[i][j] %= MOD;26             }27         }28         for (int i = 1; i <= n; i++) {29             for (int j = 1; j <= n; j++) {30                 scanf("%d", &B[i][j]);31                 B[i][j] %= MOD;32             }33         }34         for (int i = 1; i <= n; i++) {35             for (int j = 1; j <= n; j++) {36                 if (A[i][j] == 1) {37                     A1[i][j-1] = 1;38                 }else if (A[i][j] == 2) {39                     A2[i][j-1] = 1;40                 }41             }42         }43         for (int j = 1; j <= n; j++) {44             for (int i = 1; i <= n; i++) {45                 if (B[i][j] == 1) {46                     B1[j][i-1] = 1;47                 }else if (B[i][j] == 2){48                     B2[j][i-1] = 1;49                 }50             }51         }52         for (int i = 1; i <= n; i++) {53             for (int j = 1; j <= n; j++) {54                 int a = (A1[i]&B1[j]).count()+(A2[i]&B2[j]).count();55                 int b = (A1[i]&B2[j]).count()+(A2[i]&B1[j]).count();56                 C[i][j] = a + b * 2;57                 printf("%d%c", C[i][j]%MOD, " \n"[j == n]);58             }59         }60     }61     return 0;62 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.