fork download
  1. #include <bits/stdc++.h>
  2. #define pi 3.14159265359
  3. using namespace std;
  4.  
  5. string solve(int K, int z, bool down) {
  6. if(K == 0) return "";
  7. for(int i =1; i <= 100000; i++) {
  8. if(1LL*i*(i+1)/2 > 1LL*K) break;
  9. if(1LL*i*(i+1)/2+(down?-i/2:0) == 1LL*K) {
  10. string ret;
  11. for(int j =0; j < i; j++) ret +=('a'+z);
  12. return ret;}
  13. }
  14. for(int i =1; i <= 100000; i++) if(1LL*i*(i+1)/2+(down?(i-1)/2:i/2)-(down?i/2:0)+1 <= K && 1LL*(i+1)*(i+2)/2+(down?i/2:(i+1)/2)-(down?(i+1)/2:0)+1 > K) {
  15. string ret;
  16. for(int j =0; j < i; j++) ret +=('a'+z);
  17. ret +=('a'+z+1);
  18. ret +=solve(K-1LL*i*(i+1)/2-(down?(i-1)/2:i/2)+(down?i/2:0)-1,z+3,true);
  19. if(ret.length() > i+1) ret[i+1] =('a'+z+2);
  20. return ret;}
  21. }
  22.  
  23. int main() {
  24. int K;
  25. cin >> K;
  26. cout << solve(K,0,false) << "\n";
  27. return 0;}
Success #stdin #stdout 0s 3480KB
stdin
1000
stdout
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcddddddefgh