Guest User

Untitled

a guest
Nov 24th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. Number of unique digits numbers 1-5324
  2. = Number of unique digits numbers 1-9
  3. + Number of unique digits numbers 10-99
  4. + Number of unique digits numbers 100-999
  5. + Number of unique digits numbers 1000-5324
  6.  
  7. f(n) = Number of unique digits numbers with length n.
  8. f(1) = 9 (1-9)
  9. f(2) = 9*9 (1-9 * 0-9 (excluding first digit))
  10. f(3) = 9*9*8 (1-9 * 0-9 (excluding first digit) * 0-9 (excluding first 2 digits))
  11. f(4) = 9*9*8*7
  12.  
  13. Number of unique digits numbers 1000-5324
  14. = Number of unique digits numbers 1000-4999
  15. + Number of unique digits numbers 5000-5299
  16. + Number of unique digits numbers 5300-5319
  17. + Number of unique digits numbers 5320-5324
  18.  
  19. N = 5324
  20.  
  21. If N[0] = 1, there are 9*8*7 possibilities for the other digits
  22. If N[0] = 2, there are 9*8*7 possibilities for the other digits
  23. If N[0] = 3, there are 9*8*7 possibilities for the other digits
  24. If N[0] = 4, there are 9*8*7 possibilities for the other digits
  25. If N[0] = 5
  26. If N[1] = 0, there are 8*7 possibilities for the other digits
  27. If N[1] = 1, there are 8*7 possibilities for the other digits
  28. If N[1] = 2, there are 8*7 possibilities for the other digits
  29. If N[1] = 3
  30. If N[2] = 0, there are 7 possibilities for the other digits
  31. If N[2] = 1, there are 7 possibilities for the other digits
  32. If N[2] = 2
  33. If N[3] = 0, there is 1 possibility (no other digits)
  34. If N[3] = 1, there is 1 possibility (no other digits)
  35. If N[3] = 2, there is 1 possibility (no other digits)
  36. If N[3] = 3, there is 1 possibility (no other digits)
  37.  
  38. uniques += (N[0]-1)*9!/(9-N.length+1)!
  39. for (int i = 1:N.length)
  40. uniques += N[i]*(9-i)!/(9-N.length+1)!
  41.  
  42. // don't forget N
  43. if (hasUniqueDigits(N))
  44. uniques += 1
  45.  
  46. binary used[10]
  47. uniques += (N[0]-1)*9!/(9-N.length+1)!
  48. used[N[0]] = 1
  49. for (int i = 1:N.length)
  50. uniques += (N[i]-sum(used 0 to N[i]))*(9-i)!/(9-N.length+1)!
  51. if (used[N[i]] == 1)
  52. break
  53. used[N[i]] = 1
  54.  
  55. // still need to remember N
  56. if (hasUniqueDigits(N))
  57. uniques += 1
  58.  
  59. Prelude> :m +Data.List
  60. Data.List> length [a | a <- [1..5324], length (show a) == length (nub $ show a)]
  61. 2939
  62.  
  63. import java.io.*;
  64. import java.util.*;
  65. import java.text.*;
  66. import java.math.*;
  67. import java.util.regex.*;
  68.  
  69. public class Solution {
  70. public static void main(String[] args) {
  71.  
  72. int rem;
  73. Scanner in=new Scanner(System.in);
  74. int num=in.nextInt();
  75. int length = (int)(Math.log10(num)+1);//This one is to find the length of the number i.e number of digits of a number
  76. // System.out.println(length);
  77. int arr[]=new int[length]; //Array to store the individual numbers of a digit for example 123 then we will store 1,2,3 in the array
  78.  
  79. int count=0;
  80. int i=0;
  81.  
  82. while(num>0) //Logic to store the digits in array
  83. { rem=num%10;
  84. arr[i++]=rem;
  85. num=num/10;
  86. }
  87. for( i=0;i<length;i++) //Logic to find the duplicate numbers
  88. {
  89. for(int j=i+1;j<length;j++)
  90. {
  91. if(arr[i]==arr[j])
  92. {
  93. count++;
  94. }
  95. }
  96. }
  97. //Finally total number of digits minus duplicates gives the output
  98. System.out.println(length-count);
  99. }
  100. }
Add Comment
Please, Sign In to add comment