fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7. public static int k, d, quantity = 0;
  8. public static long[] was = new long[40320];
  9. public static long[] nums = new long[9];
  10. public static boolean Was(long h) {
  11. long c = 0;
  12. for (int i = 0; i < quantity; i++) {
  13. if (was[i] == h) c++;
  14. }
  15. if (c == 0) {
  16. was[quantity] = h;
  17. return true;
  18. }
  19. else
  20. return false;
  21. }
  22. public static void Swap(int a, int b) {
  23. long t = nums[a];
  24. nums[a] = nums[b];
  25. nums[b] = t;
  26. }
  27. public static void Generate(int n) {
  28. if (n == k) {
  29. long sum = 0;
  30. long p = 1;
  31. long h = 0;
  32. long hash = 1;
  33. for (int i = k - 1; i >= 0; i--) {
  34. long subs = nums[i];
  35. sum += nums[i] % d * p;
  36. p *= Math.pow(10, (long)Math.log10(nums[i]) + 1);
  37. p = p % d;
  38. while (subs!= 0) {
  39. h += subs % 10 * hash;
  40. hash *= 101;
  41. subs /= 10;
  42. }
  43. }
  44. if (sum%d == 0 && Was(h) == true) {
  45. quantity++;
  46. }
  47. }
  48. else {
  49. for(int j = n; j < k;j++) {
  50. Swap(n,j);
  51. Generate(n+1);
  52. Swap(n,j);
  53. }
  54. }
  55. }
  56. public static void main (String[] args) throws java.lang.Exception
  57. {
  58. Scanner in = new Scanner(System.in);
  59. d = in.nextInt();
  60. k = in.nextInt();
  61. for(int i = 0; i < k; i++) {
  62. nums[i] = in.nextLong();
  63. }
  64. Generate(0);
  65. System.out.println(quantity);
  66. }
  67. }
Success #stdin #stdout 0.11s 2184192KB
stdin
71 8
4018916609
7495223237
3405637482
3166003637
8998228133
1141886496
9124347310
7736090711
stdout
584