Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.*;
import java.lang.*;
import java.io.*;

public class Solution {
	 static int MOD = 998244353;

    private static int factorial(int n) {
        int ans = 1;
        for (int i = 1; i <= n; i++) {
            ans = (int) ((long) ans * i % MOD);
        }
        return ans;
    }
	public static void main (String[] args) throws java.lang.Exception {
	    // Write your code here
        // Take input and print desired output
		 Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t-- > 0) {
            int p = scanner.nextInt();
            int q = scanner.nextInt();
            int r = scanner.nextInt();
            int[][][] dp = new int[p + 1][q + 1][r + 1];
            for (int i = 0; i < =p; i++) {
                for (int j = 0; j <= q; j++) {
                    for (int k = 0; k <= r; k++) {
                        if (i + j < k) {
                            dp[i][j][k] = 0;
                        } else if (i + j == k) {
                            dp[i][j][k] = factorial(i + j);
                        } else if (j == 1) {
                            dp[i][j][k] = 0;
                        } else if (k == 1) {
                            dp[i][j][k] = 1;
                        } else {
                            dp[i][j][k] = (int) (((long) i * dp[i - 1][j][k - 1] + (long) j * dp[i][j - 1][k - 1])
                                    % MOD);
                        }
                    }
                }
            }
            System.out.println(dp[p][q][r]);
        }
	
	}
}


What I have tried:

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

public class Solution {
	 static int MOD = 998244353;

    private static int factorial(int n) {
        int ans = 1;
        for (int i = 1; i <= n; i++) {
            ans = (int) ((long) ans * i % MOD);
        }
        return ans;
    }
	public static void main (String[] args) throws java.lang.Exception {
	    // Write your code here
        // Take input and print desired output
		 Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t-- > 0) {
            int p = scanner.nextInt();
            int q = scanner.nextInt();
            int r = scanner.nextInt();
            int[][][] dp = new int[p + 1][q + 1][r + 1];
            for (int i = 0; i < =p; i++) {
                for (int j = 0; j <= q; j++) {
                    for (int k = 0; k <= r; k++) {
                        if (i + j < k) {
                            dp[i][j][k] = 0;
                        } else if (i + j == k) {
                            dp[i][j][k] = factorial(i + j);
                        } else if (j == 1) {
                            dp[i][j][k] = 0;
                        } else if (k == 1) {
                            dp[i][j][k] = 1;
                        } else {
                            dp[i][j][k] = (int) (((long) i * dp[i - 1][j][k - 1] + (long) j * dp[i][j - 1][k - 1])
                                    % MOD);
                        }
                    }
                }
            }
            System.out.println(dp[p][q][r]);
        }
	
	}
}
Posted
Updated 27-Jun-23 21:59pm
Comments
Graeme_Grant 28-Jun-23 3:18am    
[moved]

Quote:
dp[i][j][k] = (int) (((long) i * dp[i - 1][j][k - 1] + (long) j * dp[i][j - 1][k - 1])
% MOD);

The above line maybe the culprit, when i (or j, or k) is 0. Are you sure the loops should start from zero (shouldn't they start from 1?)?
 
Share this answer
 
Have you checked your array indices to see which one is out of bounds?

The simplest way is to add checks for each index against the min/max dimension size. If it is out of bounds, then break and inspect why.
 
Share this answer
 
Your for loops start from 0, and run to N - that's ok, mostly because you make your array one element larger in all dimensions so it contains all of those indexes.

But ... it doesn't contain any negative indexes, so the first time through any of those loops, this code will try to access a negative addressed element and will fail with an out of bounds error:
dp[i][j][k] = (int) (((long) i * dp[i - 1][j][k - 1] + (long) j * dp[i][j - 1][k - 1]


Thirty seconds with the debugger would have shown you that!

Be aware: factorial values get very large, very quickly: 12! is the largest value you can fit in a 32 bit integer, so this code could give you some strange results very easily!
dp[i][j][k] = factorial(i + j);
 
Share this answer
 
Comments
CPallini 28-Jun-23 7:16am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900