2014 Multi-University Training Contest 5 - HDU4920 - Matrix multiplication

第五场坐火车耽误了

累加可以特判零的情况

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF (int)(1e9)
#define maxn 100010
#define Lson l, mid, rt << 1
#define Rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef long long ll;
int a[810][810], b[810][810], c[810][810];
int scanff() {
    int res = 0, ch, flag = 0;
    if ((ch = getchar()) == '-')
        flag = 1;
    else if (ch >= '0' && ch <= '9')
        res = ch - '0';
    while ((ch = getchar()) >= '0' && ch <= '9')
        res = res * 10 + ch - '0';
    return flag ? -res : res;
}
void printff(int a) {
    if (a > 9)
        printff(a / 10);
    putchar(a % 10 + '0');
}
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                a[i][j] = scanff();
                a[i][j] %= 3;
            }
        }
        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                b[i][j] = scanff();
                b[i][j] %= 3;
            }
        }
        memset(c,0,sizeof(c));
        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                if (a[i][j] == 0) continue;
                for (int k = 0; k < n; ++ k) {
                    c[i][k] += a[i][j] * b[j][k];
                }
            }
        }
        for (int i = 0; i < n; ++ i) {
            printff(c[i][0]%3);
            for (int j = 1; j < n; ++ j) {
                putchar(' '), printff(c[i][j]%3);
            }
            putchar('\n');
        }
    }
}

标程是用bitset,学习了

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF (int)(1e9)
#define maxn 100010
#define Lson l, mid, rt << 1
#define Rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef long long ll;
int a[810][810], b[810][810], c[810][810];
int scanff() {
    int res = 0, ch, flag = 0;
    if ((ch = getchar()) == '-')
        flag = 1;
    else if (ch >= '0' && ch <= '9')
        res = ch - '0';
    while ((ch = getchar()) >= '0' && ch <= '9')
        res = res * 10 + ch - '0';
    return flag ? -res : res;
}
void printff(int a) {
    if (a > 9)
        printff(a / 10);
    putchar(a % 10 + '0');
}
bitset<810> col[3][810], row[3][810];
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        for (int i = 0; i < 3; ++ i) {
            for (int j = 0; j < n; ++ j) {
                col[i][j].reset();
                row[i][j].reset();
            }
        }
        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                a[i][j] = scanff();
                a[i][j] %= 3;
                row[a[i][j]][i].set(j);
            }
        }
        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                b[i][j] = scanff();
                b[i][j] %= 3;
                col[b[i][j]][j].set(i);
            }
        }

        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                c[i][j] = 0;
                c[i][j] += (row[1][i]&col[1][j]).count();
                c[i][j] += (row[1][i]&col[2][j]).count()*2;
                c[i][j] += (row[2][i]&col[1][j]).count()*2;
                c[i][j] += (row[2][i]&col[2][j]).count();
            }
        }
        for (int i = 0; i < n; ++ i) {
            printff(c[i][0]%3);
            for (int j = 1; j < n; ++ j) {
                putchar(' '), printff(c[i][j]%3);
            }
            putchar('\n');
        }
    }
}


你可能感兴趣的:(AD,Hoc,比赛解题报告,数学&数论,HDU)