Advertisement
svetlai

BinaryTrees

Nov 13th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System.Net;
  2. using System.Numerics;
  3.  
  4. namespace BinaryTrees
  5. {
  6.     using System;
  7.     using System.Collections.Generic;
  8.     using System.Linq;
  9.     using System.Text;
  10.     using System.Threading.Tasks;
  11.  
  12.     public class Program
  13.     {
  14.         private static long[] memo;
  15.  
  16.         static long Trees(int n)
  17.         {
  18.             if (n == 0)
  19.             {
  20.                 return 1;
  21.             }
  22.  
  23.             if (memo[n] > 0)
  24.             {
  25.                 return memo[n];
  26.             }
  27.  
  28.             long result = 0;
  29.             for (int i = 0; i < n; i++)
  30.             {
  31.                 result += Trees(i) * Trees(n - 1 - i);
  32.             }
  33.  
  34.             memo[n] = result;
  35.  
  36.             return result;
  37.         }
  38.  
  39.         public static void Main()
  40.         {
  41.             var input = Console.ReadLine();
  42.             var groups = new int[26];
  43.  
  44.             foreach (var ball in input)
  45.             {
  46.                 groups[ball - 'A']++;
  47.             }
  48.  
  49.             int n = input.Length;
  50.  
  51.             memo = new long[n + 1];
  52.  
  53.             var factorials = new long[n + 1];
  54.             factorials[0] = 1;
  55.  
  56.             for (int i = 0; i < n; i++)
  57.             {
  58.                 factorials[i + 1] = factorials[i] * (i + 1);
  59.             }
  60.  
  61.             long result = factorials[n];
  62.  
  63.             foreach (var item in groups)
  64.             {
  65.                 result /= factorials[item];
  66.             }
  67.  
  68.             BigInteger total = result;
  69.             total *= Trees(n);
  70.             Console.WriteLine(total);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement