Advertisement
_rashed

UVA 10705

Feb 27th, 2022
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7.  
  8. int main()
  9. {
  10.     ios_base::sync_with_stdio(false);
  11.     cin.tie(NULL);
  12.     cout.tie(NULL);
  13.     int t;
  14.     cin >> t;
  15.     while(t--) {
  16.         ll k;
  17.         string s;
  18.         ll n;
  19.         cin >> k >> s >> n;
  20.         if(n < 0) {
  21.             n *= -1;
  22.             for(int i = 0; i < s.size(); i++) {
  23.                 s[i] = (s[i] == 'p' ? 'n':'p');
  24.             }
  25.         }
  26.         unsigned ll ans = 0;
  27.         unsigned ll req = n;
  28.         unsigned ll sum = 0;
  29.         reverse(s.begin(),s.end());
  30.         unsigned ll mx = 0;
  31.         for(unsigned ll i = 0; i < s.size(); i++) {
  32.             if(s[i] == 'p')
  33.                 mx += ((unsigned ll) 1 << i);
  34.         }
  35.         if(n > mx) {
  36.             cout << "Impossible";
  37.         }
  38.         else {
  39.             for(unsigned ll i = 0; i < s.size(); i++) {
  40.                 //cout << "req is " << req << "\n";
  41.                 if(req%2) {
  42.                     ans |= ((unsigned ll) 1 << i);
  43.                     //sum += (s[i] == 'n' ? -1:1)*((unsigned ll)1 << i);
  44.                     if(req%2 && s[i] == 'n') {
  45.                         req /= 2;
  46.                         req ++;
  47.                     }
  48.                     else {
  49.                         req /= 2;
  50.                     }
  51.                 }
  52.                 else {
  53.                     req /= 2;
  54.                 }
  55.  
  56.             }
  57.             for(unsigned ll i = s.size()-1; i >= 0; i--) {
  58.                 cout << (ans&((unsigned ll)1 << i) ? '1':'0');
  59.                 if(!i)
  60.                     break;
  61.             }
  62.         }
  63.  
  64.         cout << "\n";
  65.     }
  66.     return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement