Advertisement
Ahmed_Negm

Untitled

Mar 29th, 2023
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35.  
  36. bool comp(string& a, string& b) {
  37.     return sz(a) > sz(b);
  38. }
  39.  
  40. set<string>st;
  41. void cnt_subseq_of_lenk(ll i,string &s,string ans=""){
  42.     if(i==sz(s)){
  43.         st.insert(ans);
  44.         return;
  45.     }
  46.   cnt_subseq_of_lenk(i+1,s,ans);
  47.   cnt_subseq_of_lenk(i+1,s,ans+s[i]);
  48. }
  49.  
  50.  
  51. void solve(){
  52. ll n,k; cin>>n>>k;
  53.  
  54. long double x = 1LL<<n;
  55. if(k>x){
  56.   cout<<0<<nl;
  57.   return;
  58. }
  59.  
  60. string s; cin>>s;
  61. cnt_subseq_of_lenk(0,s);
  62. vector<string>v(all(st));
  63. sort(all(v),comp);
  64. ll ans = 0;
  65. for(int i=0;i<sz(v);i++){
  66.     if(!k)break;
  67.     k--;
  68.     ans+=sz(s)-sz(v[i]);
  69. }
  70. if(k) ans = -1;
  71. cout<<ans<<nl;
  72.  
  73.  
  74. }
  75.  
  76. int main(){
  77.     Fast_IO();
  78. int t =1;
  79. //cin>>t;
  80. while(t--){
  81. solve();
  82. }
  83. return 0;
  84. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement