fork download
  1. #include <bits/stdc++.h>
  2. #define FNAME ""
  3. using namespace std;
  4. const int MAXN = 501;
  5. typedef long long ll;
  6. const ll MOD = 1e9 + 7;
  7.  
  8. void fastip() {
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0); cout.tie(0);
  11. if (fopen(FNAME".inp", "r")) {
  12. freopen(FNAME".inp", "r", stdin);
  13. freopen(FNAME".out", "w", stdout);
  14. }
  15. }
  16.  
  17. vector<string> beautiful_number;
  18. vector<long long> correct_number;
  19. string s = "";
  20. long long n;
  21.  
  22. void init(){
  23. beautiful_number.push_back("1");
  24. beautiful_number.push_back("22");
  25. beautiful_number.push_back("333");
  26. beautiful_number.push_back("4444");
  27. beautiful_number.push_back("55555");
  28. beautiful_number.push_back("666666");
  29. beautiful_number.push_back("7777777");
  30. beautiful_number.push_back("88888888");
  31. beautiful_number.push_back("999999999");
  32. }
  33.  
  34. void backtrack(int i,int sum){
  35. if(sum + i + 1 > 10){
  36. return;
  37. }
  38. for(int j = i ; j < 9 ; j++){
  39. if(sum + j + 1 <= 10){
  40. string tmp = s;
  41. s += beautiful_number[j];
  42. string k = s;
  43. beautiful_number.push_back(k);
  44. while(next_permutation(k.begin(),k.end())){
  45. beautiful_number.push_back(k);
  46. }
  47. backtrack(j + 1, sum + j + 1);
  48. s = tmp;
  49. }
  50. }
  51. }
  52.  
  53. int main(){
  54. fastip();
  55.  
  56. init();
  57. backtrack(0,0);
  58.  
  59. cin >> n;
  60.  
  61. for(int i = 0; i < beautiful_number.size() ; i++){
  62. string k = beautiful_number[i];
  63. correct_number.push_back(stoll(k));
  64. }
  65.  
  66. sort(correct_number.begin(),correct_number.end());
  67.  
  68. int pos = upper_bound(correct_number.begin(),correct_number.end(), n + 1) - correct_number.begin();
  69. cout << correct_number[pos];
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
22