Advertisement
Zeinab_Hamdy

Untitled

Oct 11th, 2023
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV  return void
  10. // #define inf 2000000000
  11. #define sz(x) int(x.size())
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define Mini(x) *min_element(all(x))
  15. #define Maxi(x) *max_element(all(x))
  16. #define fixed(n) fixed << setprecision(n)
  17. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  18. #define cin(v) for (auto&i:v) cin >> i;
  19. #define cout(v) for (auto&i:v) cout << i << " ";
  20. #define clr(memo, x) memset(memo, x, sizeof memo)
  21. #define updmin(a, b) a = min(a, b)
  22. #define updmax(a, b) a = max(a, b)
  23. #define vi vector < int >
  24. #define vl vector < ll >
  25. #define vc vector < char >
  26. #define vs vector < string >
  27. #define v2i vector < vector < int > >
  28. #define v2l vector < vector < int > >
  29. #define seti set < int >
  30. #define setl set < ll >
  31. #define mapii map < int , int >
  32. #define mapll map < ll , ll >
  33. #define mapli map < ll , int >
  34. #define mapci map < char , int >
  35. #define mapsi map < string , int >
  36. #define pll pair < ll , ll >
  37. #define pii pair < int , int >
  38. #define range(l,r,x) for(int i=l ; i < r ; i+=x)
  39. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  40. vector < string > ternary= {"NO\n" , "YES\n"};
  41.  
  42. void  Zainab(){
  43.             #ifndef ONLINE_JUDGE
  44.               freopen("input.txt", "r", stdin);
  45.               freopen("output.txt", "w", stdout);
  46.             #endif
  47. }
  48.  
  49.  
  50. /*================================  solution  ================================ */
  51.  
  52. ll dp [13][2][2];
  53.  
  54. string S ;
  55.  
  56.  
  57.  
  58. ll rec(int idx , bool sm , bool leading){
  59.     if(idx >= sz(S)) return !leading ;
  60.    
  61.    
  62.     ll & ret = dp[idx][sm][leading];
  63.     if(~ret)
  64.         return ret;
  65.    
  66.  
  67.  
  68.    ret=0;
  69.  
  70.    for(int i = 0 ; i <= 9 ; i++){
  71.         if(i == 4) continue;
  72.         if(i + '0' > S[idx] and !sm) continue;
  73.        
  74.         if(i == 0 and leading)
  75.             ret += rec(idx+1 , sm or ( i +'0' < S[idx]) , leading);
  76.         else
  77.             ret += rec(idx+1 , sm or ( i +'0' < S[idx]) , false);
  78.     }
  79.    
  80.     return ret ;
  81. }
  82.  
  83. void build(int idx , bool sm , bool leading , ll sum ){
  84.     if(idx >= sz(S)) return ;
  85.  
  86.     for(int i = 0 ; i <= 9 ; i++){
  87.         if(i == 4) continue;
  88.         if(i + '0' > S[idx] and !sm) continue;
  89.  
  90.         ll ret =0;  
  91.         if(i == 0 and leading)
  92.             ret = rec(idx+1 , sm or ( i +'0' < S[idx]) , leading);
  93.         else
  94.             ret = rec(idx+1 , sm or ( i +'0' < S[idx]) , false);
  95.  
  96.         if(ret < sum ) sum -=ret ;
  97.         else {
  98.             if(i or leading) cout << i ;
  99.            
  100.             if(i == 0 and leading){
  101.                 build(idx+1 , sm or ( i +'0' < S[idx]) , leading , sum);
  102.             }
  103.             else {
  104.                 build(idx+1 , sm or ( i +'0' < S[idx]) , false , sum);
  105.             }
  106.            return ;
  107.         }
  108.     }
  109. }
  110.  
  111. void myCode(){
  112.  
  113.  
  114. ll k;
  115. cin >> k ;
  116.  
  117.  
  118. S= to_string(k);
  119. while(sz(S) !=13) S="0" + S;
  120.  
  121. clr(dp , -1);
  122. build(0,0,0 ,k);
  123. cout << nl;
  124.  
  125.  
  126.  
  127. }
  128.  
  129.  
  130. int main(){
  131.  
  132. FastCode ;
  133. Zainab() ;
  134.  
  135.  
  136.  
  137.  
  138.  
  139.             int testCase=1;
  140.                 cin >> testCase ;
  141.             for(int i=1 ; i<= testCase ; i++){
  142.                 //  cout << "Case #" << i << ": ";
  143.                 myCode();
  144.             }
  145.      
  146.  
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement