fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct nris {int n, r, *is; void (*callback)(const struct nris *); void *context;};
  4. struct nris *create_nris(int n, int r, void (*callback)(const struct nris *), void *context) {
  5. if (n < 1 || n < r || !callback) return 0;
  6. struct nris *p = malloc(sizeof (struct nris));
  7. p->n = n;p->r = r;p->callback = callback;p->context = context;
  8. p->is = malloc(sizeof (int) * r);
  9. return p;
  10. }
  11. void delete_nris(struct nris *p) {if (p) {free(p->is);free(p);}}
  12. struct nris *ui2is(struct nris *p, unsigned int ui) {
  13. int i = 0, j;
  14. if (p) for (j = 0; j < p->n; j++) if (ui & (1 << j)) p->is[i++] = j;
  15. return p;
  16. }
  17. void permute(struct nris *p, int l, int r) {
  18. int i, tmp;
  19. if (!p) return;
  20. else if (l == r) p->callback(p);
  21. else {
  22. for (i = l; i <= r; i++) {
  23. #define SWAP(t, p, q) (t = *(p), *(p) = *(q), *(q) = t)
  24. SWAP(tmp, p->is + l, p->is + i);
  25. permute(p, l + 1, r);
  26. SWAP(tmp, p->is + l, p->is + i);
  27. #undef SWAP
  28. }
  29. }
  30. }
  31. void comb(struct nris *p, int need, unsigned int chosen, int at) {
  32. if (!p || p->n < need + at) return;
  33. if (!need) {
  34. ui2is(p, chosen);
  35. permute(p, 0, p->r - 1);
  36. return;
  37. }
  38. comb(p, need - 1, chosen | (1 << at), at + 1);
  39. comb(p, need, chosen, at + 1);
  40. }
  41. void permutations_do(int n, int r, void (*callback)(const struct nris *), void *context) {
  42. struct nris *nris = create_nris(n, r, callback, context);
  43. comb(nris, nris->r, 0, 0);
  44. delete_nris(nris);
  45. }
  46.  
  47. int _a0b(const int *begin, const int *end, int *a, int *b) {
  48. #define sep 0
  49. if (*begin == sep || *(end - 1) == sep) return 0;
  50. register const int *p;
  51. for (p = begin, *a = 0; *p != sep && p < end; p++) *a = *a * 10 + *p;
  52. for (p = p + 1, *b = 0; *p != sep && p < end; p++) *b = *b * 10 + *p;
  53. #undef sep
  54. return 1;
  55. }
  56. void f8872(const struct nris *p) {
  57. int a, b;
  58. if (_a0b(p->is, p->is + p->r, &a, &b) && a * 8 == b) printf("%d/%d\n", a, b);
  59. }
  60. void f8883(const struct nris *p) {
  61. int a, b;
  62. if (_a0b(p->is, p->is + p->r, &a, &b) && a - b == 44444) printf("%d-%d\n", a, b);
  63. }
  64. int main() {
  65. permutations_do(10, 10, f8872, NULL);
  66. permutations_do(10, 10, f8883, NULL);
  67. return 0;
  68. }
  69.  
Success #stdin #stdout 0.13s 2300KB
stdin
Standard input is empty
stdout
3187/25496
4589/36712
4591/36728
4689/37512
4691/37528
4769/38152
5237/41896
5371/42968
5789/46312
5791/46328
5839/46712
5892/47136
5921/47368
5916/47328
6479/51832
6741/53928
6789/54312
6791/54328
6839/54712
7123/56984
7312/58496
7364/58912
7421/59368
7416/59328
7894/63152
7941/63528
8174/65392
8179/65432
8394/67152
8439/67512
8419/67352
8932/71456
8942/71536
8953/71624
8954/71632
9156/73248
9158/73264
9182/73456
9321/74568
9316/74528
9352/74816
9421/75368
9416/75328
9523/76184
9531/76248
9541/76328
46279-1835
46297-1853
46819-2375
46981-2537
47629-3185
47962-3518
48169-3725
48196-3752
49627-5183
49681-5237
49762-5318
49816-5372
51238-6794
51427-6983
52138-7694
53416-8972
54316-9872
54127-9683