• hdu4920Matrix multiplication (矩阵,bitset)


    Problem Description
    Given two matrices A and B of size n×n, find the product of them.

    bobo hates big integers. So you are only asked to find the result modulo 3.
     

    Input
    The input consists of several tests. For each tests:

    The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).
     

    Output
    For each tests:

    Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
     

    Sample Input
    1 0 1 2 0 1 2 3 4 5 6 7
     

    Sample Output
    0 0 1 2 1
     
    题意:给你两个矩阵,让你把它们相乘后输出%3后的结果。
    思路:普通的矩阵乘法+优化能过的,还有一种思路是开bitset<1000>bt[3],ct[3],储存每一行mod3后为0,1,2的情况,那么1*1=1,1 *2=2,2*1=2,2*2=1.
    代码一:普通矩阵乘法+优化
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<bitset>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef long double ldb;
    #define inf 99999999
    #define pi acos(-1.0)
    #define MOD 3
    int n,m;
    int data[4][805][805];
    
    void solve()
    {
        int i,j,k;
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                data[3][i][j]=0;
            }
        }
        for(i=0;i<n;i++){
            for(k=0;k<m;k++){
                if(data[1][i][k]>0){
                    for(j=0;j<m;j++){
                        data[3][i][j]=data[3][i][j]+data[1][i][k]*data[2][k][j];
                    }
                }
            }
        }
    }
    
    int main()
    {
        int i,j;
        while(scanf("%d",&n)!=EOF)
        {
            m=n;
            for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                    scanf("%d",&data[1][i][j]);
                    data[1][i][j]%=3;
                }
            }
    
            for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                    scanf("%d",&data[2][i][j]);
                    data[2][i][j]%=3;
                }
            }
    
            solve();
            for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                    if(j<n-1)printf("%d ",data[3][i][j]%3);
                    else printf("%d
    ",data[3][i][j]%3);
                }
            }
        }
        return 0;
    }

    代码二:用bitset
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<bitset>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef long double ldb;
    #define inf 99999999
    #define pi acos(-1.0)
    #define MOD 3
    #define maxn 805
    bitset<1000>bt[maxn][3],ct[maxn][3];
    
    
    int main()
    {
        int n,m,i,j,c;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=1;i<=n;i++){
                for(j=0;j<3;j++){
                    bt[i][j].reset();
                    ct[i][j].reset();
                }
            }
            for(i=1;i<=n;i++){
                for(j=1;j<=n;j++){
                    scanf("%d",&c);
                    bt[i][c%3].set(j);
                }
            }
            for(i=1;i<=n;i++){
                for(j=1;j<=n;j++){
                    scanf("%d",&c);
                    ct[j][c%3].set(i);
                }
            }
            for(i=1;i<=n;i++){
                for(j=1;j<=n;j++){
                    c=((bt[i][1]&ct[j][1]).count()+(bt[i][1]&ct[j][2]).count()*2+(bt[i][2]&ct[j][1]).count()*2+(bt[i][2]&ct[j][2]).count() )%3;
                    if(j==n)printf("%d
    ",c);
                    else printf("%d ",c);
                }
            }
        }
        return 0;
    }
    


  • 相关阅读:
    mmap文件修改内容的写回
    信号处理之物理信号和软件信号
    从printXX看tty设备(5)串口终端
    从printXX看tty设备(3)键盘输入处理
    LeetCode——Hamming Distance
    LeetCode——Add Strings
    计算树的高度和节点的个数
    LeetCode——Diameter of Binary Tree
    LeetCode——Number of Boomerangs
    九大排序算法总结
  • 原文地址:https://www.cnblogs.com/herumw/p/9464532.html
Copyright © 2020-2023  润新知