(SPOJ) Matrizes - Solution

Link to the problem: http://br.spoj.com/problems/MATRIZ2/

The solution below multiplies matrix A and matrix B in order to get only the answer for the required position. There is no need to calculate all the positions of matrix C.


import java.io.*;
import java.util.*;

class solucao {
    public static int reader(BufferedReader br) throws NumberFormatException, IOException {     
        int n;
        int resp = 0;     
      
        while (true) {         
            n = br.read();         
            if (n >= '0' && n <= '9') {
                break;
            }
        }
           
        while (true) {         
            resp = resp*10 + n-'0';         
            n = br.read();         
            if (n < '0' || n > '9') {
                break;     
            }
        }
      
        return resp;     
    }
   
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        int size = reader(br);

        int P = reader(br);
        int Q = reader(br);
        int R = reader(br);
        int S = reader(br);
        int X = reader(br);
        int Y = reader(br);
       
        int row = reader(br);
        int col = reader(br);
       
        long result = 0;
        // multiply all the elements in line I by the elements in column J
        for (int i = 1; i <= size; i++) {
            result += (((P*row+Q*i)%X) * ((R*i+S*col)%Y));
        }
       
        bw.write(result+"\n");
           
        bw.flush();
        bw.close();
       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        solucao m = new solucao();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução