SHARE
TWEET

Untitled

a guest Oct 12th, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int d[31][31][31];
  2. int NO_ANSWER = -1;
  3.  
  4. int findAnswer(int a, int b, int c, int m) {
  5.     if (d[a][b][c] != NO_ANSWER)
  6.         return d[a][b][c];
  7.  
  8.     if (a <= 0 || b <= 0 || c <= 0)
  9.         return d[a][b][c] = 1;
  10.  
  11.     if (a > 30)
  12.         findAnswer(30, b, c, m);
  13.  
  14.     if (b > 30)
  15.         findAnswer(a, 30, c, m);
  16.  
  17.     if (c > 30)
  18.         findAnswer(a, b, 30, m);
  19.  
  20.     if (a > b && a > c) {
  21.         d[a][b][c] = (findAnswer(a - 2, b, c, m) + findAnswer(a - 1, b - 1, c, m) + findAnswer(a - 1, b, c - 1, m)) % m;
  22.     }
  23.     else {
  24.         if (b > a && b > c) {
  25.             d[a][b][c] = (findAnswer(a, b - 2, c, m) + findAnswer(a, b - 1, c, m) + findAnswer(a, b - 1, c - 1, m)) % m;
  26.         }
  27.         else {
  28.             if (c > a && c > b) {
  29.                 d[a][b][c] = (findAnswer(a, b, c - 3, m) + findAnswer(a - 1, b, c - 1, m) + findAnswer(a, b - 1, c - 1, m)) % m;
  30.             }
  31.             else {
  32.                 d[a][b][c] = (findAnswer(a - 1, b, c, m) + findAnswer(a, b - 1, c, m) + findAnswer(a, b, c - 1, m)) % m;
  33.             }
  34.         }
  35.     }
  36.     return d[a][b][c];
  37. }
  38.  
  39. int main() {   
  40.     int a, b, c, m;
  41.     scanf("%d %d %d %d", &a, &b, &c, &m);
  42.  
  43.     for (int i = 0; i < 31; i++) {
  44.         for (int j = 0; j < 31; j++) {
  45.             for (int k = 0; k < 31; k++) {
  46.                 d[i][j][k] = NO_ANSWER;
  47.             }
  48.         }
  49.     }
  50.  
  51.     printf("%d", findAnswer(a, b, c, m));
  52. }
RAW Paste Data
Top