Advertisement
Josif_tepe

Untitled

Oct 26th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. ll power(ll a, ll b, ll x){
  7.     ll result = 1;
  8.     while(b > 0) {
  9.         if(result * a > x) {
  10.             result *= a;
  11.             break;
  12.         }
  13.         result *= a;
  14.         b--;
  15.     }
  16.     return result;
  17. }
  18. int main() {
  19.     ll n, m, k, x;
  20.     cin >> n >> m >> k >> x;
  21.     vector<string> v;
  22.     string S;
  23.     cin >> S;
  24.    
  25.     for(int i = 0; i < m; i++) {
  26.         string tmp;
  27.         cin >> tmp;
  28.         sort(tmp.begin(), tmp.end());
  29.         v.push_back(tmp);
  30.     }
  31.     vector<char> result;
  32.     for(ll i = 0; i < m; i++) {
  33.         int j = 0;
  34.         ll k_power = power(k, m - i - 1, x);
  35.         ll sum = 0;
  36.         while(sum + k_power < x) {
  37.             sum += k_power;
  38.             j++;
  39.         }
  40.         x -= sum;
  41.         result.push_back(v[i][j]);
  42.     }
  43.     int idx = 0;
  44.     for(int i = 0; i < S.size(); i++) {
  45.         if(S[i] == '#') {
  46.             S[i] = result[idx];
  47.             idx++;
  48.         }
  49.     }
  50.     cout << S << endl;
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement