fork(1) download
  1. // cas O((N+1)! lg N)
  2. #include <cstdio>
  3. #include <set>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. int main() {
  9. int N;
  10. scanf(" %d",&N);
  11. vector<pair<long long,long long> > f(N);
  12. vector<int> p(N); // pouzita permutacia
  13. for(int i =0; i < N; i++) {
  14. p[i] =i;
  15. long long a;
  16. scanf(" %lld",&a);
  17. f[i].second =0LL;
  18. f[i].first =a;
  19. // zratam pocet cifier fragmentu
  20. while(a > 0LL) {a /=10LL; f[i].second++;}}
  21. set<long long> odp;
  22. while(true) {
  23. // v lubovolnom poradi na seba nalepim fragmenty
  24. long long a =0LL,b =1LL;
  25. for(int i =0; i < N; i++) {
  26. a +=f[p[i]].first*b;
  27. for(int j =0; j < f[p[i]].second; j++) b *=10LL;}
  28. odp.insert(a);
  29. if(!next_permutation(p.begin(),p.end())) break;}
  30. for(set<long long>::iterator it =odp.begin(); it != odp.end(); it++) printf("%lld\n",*it);
  31. return 0;}
Success #stdin #stdout 0s 2968KB
stdin
3
78 5 7878
stdout
5787878
7857878
7878578
7878785