fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. bool cmp(int x, int y) { return x > y; }
  8.  
  9. int main () {
  10. int X, Y, n = 0, N, a = 0, c = 1;
  11. scanf("%d%d", &X, &N);
  12. Y = X;
  13.  
  14. while (Y != 0) {
  15. Y /= 10;
  16. n++;
  17. }
  18.  
  19. int *myints = new int[n];
  20.  
  21. for (int i = n-1; i >= 0; i--) {
  22. myints[i] = X%10;
  23. X /= 10;
  24. }
  25.  
  26. std::sort (myints,myints+n);
  27.  
  28. do {
  29. if (myints[0] != 0) {
  30. for(int i=n-1; i>=0; i--){
  31. a += myints[i]*c;
  32. c *= 10;
  33. }
  34.  
  35. if (a % N == 0) {
  36. cout << a;
  37.  
  38. return 0;
  39. }
  40.  
  41. a = 0;
  42. c = 1;
  43.  
  44. }
  45. } while ( std::next_permutation(myints,myints+n) );
  46.  
  47.  
  48.  
  49.  
  50. delete [] myints;
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 3232KB
stdin
74530 4
stdout
35704