| Task: | Ruudukko | 
| Sender: | Septicuss | 
| Submission time: | 2022-10-31 04:19:13 +0200 | 
| Language: | Java | 
| Status: | READY | 
| Result: | 28 | 
| group | verdict | score | 
|---|---|---|
| #1 | ACCEPTED | 28 | 
| #2 | WRONG ANSWER | 0 | 
| #3 | WRONG ANSWER | 0 | 
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.07 s | 1, 2, 3 | details | 
| #2 | ACCEPTED | 0.07 s | 1, 2, 3 | details | 
| #3 | ACCEPTED | 0.07 s | 1, 2, 3 | details | 
| #4 | ACCEPTED | 0.11 s | 2, 3 | details | 
| #5 | WRONG ANSWER | 0.54 s | 2, 3 | details | 
| #6 | WRONG ANSWER | 0.51 s | 2, 3 | details | 
| #7 | ACCEPTED | 0.39 s | 3 | details | 
| #8 | TIME LIMIT EXCEEDED | -- | 3 | details | 
| #9 | TIME LIMIT EXCEEDED | -- | 3 | details | 
Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Objects;
import java.util.StringTokenizer;
public class D430 {
	static final long mod = 1000000007;
	static HashMap<Move, Long> cache = new HashMap<>();
	static int n;
	static long c = 0;
	static long[][] grid;
	public static void main(String[] args) {
		FastReader reader = new FastReader();
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
		n = reader.nextInt();
		grid = new long[n][n];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				grid[i][j] = reader.nextInt();
		reader.close();
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++) {
				if (grid[i][j] == 1) {
					c += 1;
					continue;
				}
//				System.out.prlongln(grid[i][j] + " produced " + sumFrom(i, j));
				c += (sumFrom(i, j) + 1) % mod;
			}
		if (c == 0)
			c = 1;
		writer.println(c);
		writer.flush();
		writer.close();
	}
	/*-
	 * GRID HAS X, Y
	 * 
	 *        x ->
	 * 		0 1 2
	 *   y	1 A B
	 * 	 ⬇  2 C D
	 */
	static long sumFrom(int y, int x) {
		final Move move = new Move(y, x);
		if (cache.containsKey(move)) return cache.get(move);
		
		long value = grid[y][x];
		long sum = 0;
		if (value == 1)
			return 1;
		for (int i = 0; i < n; i++) {
			long horizontal = grid[y][i];
			long vertical = grid[i][x];
			if (horizontal < value) {
				long toAdd = horizontal == 1 ? 1 : sumFrom(y, i) + 1;
				sum += toAdd;
			}
			if (vertical < value) {
				long toAdd = vertical == 1 ? 1 : sumFrom(i, x) + 1;
				sum += toAdd;
			}
		}
		
		cache.put(move, sum);
		return sum;
	}
	static class Move {
		int y;
		int x;
		public Move(int y, int x) {
			this.y = y;
			this.x = x;
		}
		@Override
		public int hashCode() {
			return Objects.hash(x, y);
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Move other = (Move) obj;
			return x == other.x && y == other.y;
		}
	}
	static class FastReader {
		private BufferedReader br;
		private StringTokenizer st;
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		void close() {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			st = null;
			br = null;
		}
		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		BigInteger nextBigInteger() {
			return new BigInteger(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}Test details
Test 1
Group: 1, 2, 3
Verdict: ACCEPTED
| input | 
|---|
| 3 1 1 1 1 1 1 1 1 1  | 
| correct output | 
|---|
| 9 | 
| user output | 
|---|
| 9 | 
Test 2
Group: 1, 2, 3
Verdict: ACCEPTED
| input | 
|---|
| 3 1 2 3 6 5 4 7 8 9  | 
| correct output | 
|---|
| 135 | 
| user output | 
|---|
| 135 | 
Test 3
Group: 1, 2, 3
Verdict: ACCEPTED
| input | 
|---|
| 3 7 8 1 4 5 4 3 9 6  | 
| correct output | 
|---|
| 57 | 
| user output | 
|---|
| 57 | 
Test 4
Group: 2, 3
Verdict: ACCEPTED
| input | 
|---|
| 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...  | 
| correct output | 
|---|
| 10000 | 
| user output | 
|---|
| 10000 | 
Test 5
Group: 2, 3
Verdict: WRONG ANSWER
| input | 
|---|
| 100 1 2 3 4 5 6 7 8 9 10 11 12 13 ...  | 
| correct output | 
|---|
| 187458477 | 
| user output | 
|---|
| -11310794129 | 
Test 6
Group: 2, 3
Verdict: WRONG ANSWER
| input | 
|---|
| 100 2995 8734 1018 2513 7971 5063 ...  | 
| correct output | 
|---|
| 964692694 | 
| user output | 
|---|
| 577446994879 | 
Test 7
Group: 3
Verdict: ACCEPTED
| input | 
|---|
| 1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...  | 
| correct output | 
|---|
| 1000000 | 
| user output | 
|---|
| 1000000 | 
Test 8
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input | 
|---|
| 1000 1 2 3 4 5 6 7 8 9 10 11 12 13 ...  | 
| correct output | 
|---|
| 229147081 | 
| user output | 
|---|
| (empty) | 
Test 9
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input | 
|---|
| 1000 520283 805991 492643 75254 527...  | 
| correct output | 
|---|
| 951147313 | 
| user output | 
|---|
| (empty) | 
