Advertisement
KoMeDiAnT

Happy Tickets

Apr 23rd, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7.  
  8. namespace Tickets
  9. {
  10.     class Solver
  11.     {
  12.         public static BigInteger Zero = BigInteger.Zero;
  13.         public static BigInteger One = BigInteger.One;
  14.         public static BigInteger NegativOne = BigInteger.MinusOne;
  15.  
  16.         static BigInteger QuantityHappyTickets(int len, int sum, BigInteger[,] memory)
  17.         {
  18.             if (memory[len, sum] >= Zero)
  19.             {
  20.                 return memory[len, sum];
  21.             }
  22.  
  23.             if (len == 0)
  24.             {
  25.                 return sum == 0 ? One : Zero;
  26.             }
  27.  
  28.             memory[len, sum] = Zero;
  29.  
  30.             for (int i = 0; i < 10; i++)
  31.             {
  32.                 if (sum - i < 0) continue;
  33.                 memory[len, sum] = BigInteger.Add(memory[len, sum], QuantityHappyTickets(len - 1, sum - i, memory));
  34.             }
  35.  
  36.             return memory[len, sum];
  37.         }
  38.  
  39.         public static BigInteger Solve(int totalLen, int totalSum)
  40.         {
  41.             var memory = new BigInteger[51, 1001];
  42.  
  43.             if (totalSum%2 != 0)
  44.             {
  45.                 return 0;
  46.             }
  47.  
  48.             totalSum /= 2;
  49.  
  50.             for (int i = 0; i < totalLen + 1; i++)
  51.             {
  52.                 for (int j = 0; j < totalSum + 1; j++)
  53.                 {
  54.                     memory[i, j] = NegativOne;
  55.                 }
  56.             }
  57.  
  58.             return QuantityHappyTickets(totalLen, totalSum, memory)*QuantityHappyTickets(totalLen, totalSum, memory);
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement