Advertisement
RaFiN_

cf-431D

Jan 9th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. /* the number of ones between n+1 to 2n increases with n...so given a certain number m  we can find which n and 2n have m numbers with exactly k digits one by binary search and digit dp */
  2.  
  3.  
  4. /*********************** let the play begin() ***********************************/
  5.  
  6. vl v1,v2;ll dp[70][2][2][70],m,k;int lim;
  7.  
  8. ll giveres(int pos,int low,int high,ll pk)
  9. {
  10.     if(pk>k)return 0ll;
  11.     if(pos==lim)
  12.     {
  13.         if(pk==k)return 1ll;
  14.         return 0ll;
  15.     }
  16.  
  17.     ll &ret = dp[pos][low][high][pk];
  18.  
  19.     if(ret!=-1)return ret;
  20.  
  21.     ret = 0;
  22.     int h = 1,l = 0;
  23.     if(high)h = v2[pos];
  24.     if(low)l = v1[pos];
  25.  
  26.     for(int i = l;i<=h;i++)
  27.     {
  28.         ret+=giveres(pos+1,low&(i==l),high&(i==h),pk+i);
  29.     }
  30.  
  31.     return ret;
  32. }
  33.  
  34. int main()
  35. {
  36.     booster()
  37.     ///read("input.txt");
  38.  
  39.  
  40.     cin>>m>>k;
  41.  
  42.     ll low = 1,high = 1e18,mid;
  43.     ll ans = -1;
  44.     while(low<=high)
  45.     {
  46.         mid = (low+high)>>1;
  47.         ll x = mid;v1.clear();v2.clear();
  48.         ll y = 2*x;
  49.         x++;
  50.         while(x)
  51.         {
  52.             v1.pb(x%2);
  53.             x/=2;
  54.         }
  55.         while(y)
  56.         {
  57.             v2.pb(y%2);
  58.             y/=2;
  59.         }
  60.         while(v1.size()<v2.size())v1.pb(0);
  61.         reverse(all(v1));
  62.         reverse(all(v2));
  63.         lim = v1.size();
  64.         mem(dp,-1);
  65.         if(giveres(0,1,1,0)>=m)
  66.         {
  67.             ans = mid;
  68.             high = mid - 1;
  69.         }
  70.         else low = mid + 1;
  71.         ///cout<<mid<<" "<<2*mid<<" "<<giveres(0,1,1,0)<<endl;
  72.  
  73.     }
  74.  
  75.     cout<<ans;
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement