zoukankan      html  css  js  c++  java
  • hdu

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learning something about matrix, so he decided to make a crazy problem for her.

    Bob has a six-faced dice which has numbers 0, 1, 2, 3, 4 and 5 on each face. At first, he will choose a number N (4 <= N <= 1000), and for N times, he keeps throwing his dice for K times (2 <=K <= 6) and writes down its number on the top face to make an N*K matrix A, in which each element is not less than 0 and not greater than 5. Then he does similar thing again with a bit difference: he keeps throwing his dice for N times and each time repeat it for K times to write down a K*N matrix B, in which each element is not less than 0 and not greater than 5. With the two matrix A and B formed, Alice’s task is to perform the following 4-step calculation.

    Step 1: Calculate a new N*N matrix C = A*B.
    Step 2: Calculate M = C^(N*N).
    Step 3: For each element x in M, calculate x % 6. All the remainders form a new matrix M’.
    Step 4: Calculate the sum of all the elements in M’.

    Bob just made this problem for kidding but he sees Alice taking it serious, so he also wonders what the answer is. And then Bob turn to you for help because he is not good at math.

    InputThe input contains several test cases. Each test case starts with two integer N and K, indicating the numbers N and K described above. Then N lines follow, and each line has K integers between 0 and 5, representing matrix A. Then K lines follow, and each line has N integers between 0 and 5, representing matrix B.

    The end of input is indicated by N = K = 0.OutputFor each case, output the sum of all the elements in M’ in a line.Sample Input
    4 2
    5 5
    4 4
    5 4
    0 0
    4 2 5 5
    1 3 1 5
    6 3
    1 2 3
    0 3 0
    2 3 4
    4 3 2
    2 5 5
    0 5 0
    3 4 5 1 1 0
    5 3 2 3 3 2
    3 1 5 4 5 2
    0 0
    Sample Output
    14
    56

    题意 :按照题目所给的要求,求最终答案
    思路 : 在一个结构体中去存一个二维矩阵时,最多可以开到 f[800][800],在往大就会直接停止运行了。
        解决此问题有一个关键的地方就是 A*B^(n*n), 这样求的话 A*B 就是1000*1000的矩阵,指定是 超时,如何展开 A*B*A*B*A*B……A*B,等于 A*(B*A)^(n*n-1),转变成为了一个 6 * 6 的矩阵。

    代码 :
    int a[1005][10];
    int b[10][1005];
    struct mat
    {
        int a[6][6];
    };
    int k;
    int c[1005][10];
    
    mat mul(mat a, mat b){
        mat r;
        memset(r.a, 0, sizeof(r.a));
        
        for(int i = 0; i < k; i++){
            for(int f = 0; f < k; f++){
                if(a.a[i][f]){
                    for(int j = 0; j < k; j++){
                        if(b.a[f][j]){
                            r.a[i][j] += a.a[i][f]*b.a[f][j];
                            r.a[i][j] %= 6;
                        }
                    }
                }
            }
        }
        return r;
    }
    
    mat pow(mat a, int n){
        mat b;
        memset(b.a, 0, sizeof(b.a));
        for(int i = 0; i < k; i++) b.a[i][i] = 1;
        
        while(n){
            if(1 & n) b = mul(b, a);
            a = mul(a, a);
            n >>= 1;
        }
        return b;
    }
    
    int main() {
        int n;
        
        while(~scanf("%d%d", &n, &k)&& n+k ){
            for(int i = 0; i < n; i++){
                for(int j = 0; j < k; j++){
                    scanf("%d", &a[i][j]);
                }
            }    
            for(int i = 0; i <k; i++){
                for(int j = 0; j < n; j++){
                    scanf("%d", &b[i][j]);
                }
            }
            mat A;
            memset(A.a, 0, sizeof(A.a));
            for(int i = 0; i < k; i++){
                for(int j = 0; j < k; j++){
                    for(int f = 0; f < n; f++){
                        A.a[i][j] += b[i][f]*a[f][j];
                        A.a[i][j] %= 6;
                    }
                }
            }
            A = pow(A, n*n-1);
            memset(c, 0, sizeof(c));
            for(int i = 0; i < n; i++){
                for(int j = 0; j < k; j++){
                    for(int f = 0; f < k; f++){
                        c[i][j] += a[i][f]*A.a[f][j];
                        c[i][j] %= 6;
                    }
                }
            }
            int sum = 0; 
            for(int i = 0; i < n; i++){
                for(int j = 0; j < n; j++){
                    int t = 0;
                    for(int f = 0; f < k; f++){
                        t += c[i][f]*b[f][j];
                        t %= 6; 
                    }               
                    sum += t;
                }
            }
            printf("%d
    ", sum);
        }
    
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    akka并发编程练习
    使用selenium和chromedriver组合爬虫时,如果爬取的页面数量巨多,会出现占用内存逐渐增大知道程序崩溃的情况
    网易2017秋招编程题集合_以下代码全部来自牛客网
    牛客网_运行问题
    json和xml之间转换的好文
    Eclipse 启动时闪退问题解决方案
    关于opencv的文件配置详细内容
    第一个opencv声称图片_以及遇到的问题集锦
    好文收藏_关于find_first_not_of函数
    好文收藏readtxt_cpp
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7828608.html
Copyright © 2011-2022 走看看