Guest User

Untitled

a guest
Jul 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import java.math.BigInteger;
  2.  
  3. public class Question2858152 {
  4. final static int ncharacters = 256;
  5. final static int nsymbols = 256;
  6.  
  7. final static int max = Math.max (nsymbols,ncharacters) + 1;
  8.  
  9. final static BigInteger [] integers = new BigInteger [max];
  10. final static BigInteger [] powers = new BigInteger [max];
  11. final static BigInteger [] factorials = new BigInteger [max];
  12.  
  13. static BigInteger maxEntropy = BigInteger.TWO.pow (nsymbols);
  14.  
  15. static BigInteger permutationCount = BigInteger.ZERO;
  16. static BigInteger combinationCount = BigInteger.ZERO;
  17. static BigInteger fileCount = BigInteger.ZERO;
  18.  
  19. public static void main (String [] args) {
  20. factorials [0] = BigInteger.ONE;
  21. for (int i = 1;i < max;i++) {
  22. integers [i] = BigInteger.valueOf (i);
  23. powers [i] = integers [i].pow (i);
  24. factorials [i] = factorials [i - 1].multiply (integers [i]);
  25. }
  26.  
  27. recurse (BigInteger.ONE,ncharacters,factorials [nsymbols],factorials [ncharacters],nsymbols,ncharacters,"");
  28.  
  29. System.out.println (combinationCount + " combinations");
  30. System.out.println (permutationCount + " permutations");
  31. System.out.println (fileCount + " files");
  32. int precision = 100000000;
  33. BigInteger nfiles = integers [nsymbols].pow (ncharacters);
  34. System.out.println (fileCount.multiply (BigInteger.valueOf (precision)).add (nfiles.divide (BigInteger.TWO)).divide (nfiles).doubleValue () / precision);
  35. }
  36.  
  37. static long time = System.currentTimeMillis ();
  38.  
  39. static void recurse (BigInteger entropy,int frequency,BigInteger symbolMultiplicity,BigInteger characterMultiplicity,int symbolsLeft,int charactersLeft,String prefix) {
  40. if (frequency == 1) {
  41. symbolMultiplicity = symbolMultiplicity.divide (factorials [charactersLeft]).divide (factorials [symbolsLeft - charactersLeft]);
  42. combinationCount = combinationCount.add (BigInteger.ONE);
  43. permutationCount = permutationCount.add (symbolMultiplicity);
  44. fileCount = fileCount.add (symbolMultiplicity.multiply (characterMultiplicity));
  45. }
  46. else
  47. for (int f = 0;charactersLeft >= 0;f++,charactersLeft -= frequency) {
  48. if (entropy.compareTo (maxEntropy) <= 0)
  49. recurse (entropy,frequency - 1,symbolMultiplicity.divide (factorials [f]),characterMultiplicity,symbolsLeft - f,charactersLeft,prefix + " ");
  50. else
  51. break;
  52. if (f == 0 && symbolsLeft == nsymbols) {
  53. long now = System.currentTimeMillis ();
  54. System.out.println ("done with maximal frequency " + frequency + " (" + (now - time) / 1000 + " seconds)");
  55. time = now;
  56. }
  57. entropy = entropy.multiply (powers [frequency]);
  58. characterMultiplicity = characterMultiplicity.divide (factorials [frequency]);
  59. }
  60. }
  61. }
Add Comment
Please, Sign In to add comment