Guest User

Untitled

a guest
Feb 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3. int dp[32][32];
  4. char res[32];
  5. int main()
  6. {
  7. ifstream fin("kimbits.in");
  8. ofstream fout("kimbits.out");
  9. int N,L;
  10. long long I;
  11. fin>>N>>L>>I;
  12. //init
  13. for ( int i = 0 ;i<32 ;i++)
  14. {
  15. dp[i][0] = 1;
  16. dp[0][i] = 1;
  17. }
  18.  
  19. //Dynamic Programming
  20. for (int i = 1 ; i<=N ;i++)
  21. {
  22. for ( int j =1 ;j<=L ;j++)
  23. {
  24. dp[i][j] = dp[i-1][j]+dp[i-1][j-1];
  25. }
  26. }
  27.  
  28. //Cantor expansion ,tricky!!
  29. int k = 0;
  30. for (int i = N ; i >0 ;i--)
  31. {
  32. if (dp[i-1][L]>=I)
  33. {
  34. res[k++] = '0';
  35. }
  36. else if (dp[i-1][L]<I)
  37. {
  38. res[k++] = '1';
  39. I -= dp[i-1][L];
  40. //new Ith in the lower bits
  41. L--;
  42. //We have confirm there is one '1' ,So L--.
  43. }
  44. }
  45. fout<<res<<endl;
  46.  
  47. fin.close();
  48. fout.close();
  49. return 0;
  50. }
Add Comment
Please, Sign In to add comment