Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <string.h>
- #include <queue>
- #include <map>
- #include <set>
- #include <deque>
- #include <cmath>
- #include <sstream>
- #include <stack>
- #include <cassert>
- #define pb push_back
- #define mp make_pair
- #define sqr(x) (x)*(x)
- using namespace std;
- const int MAXN = 200500;
- int k,n;
- char s[MAXN];
- int r;
- int chislo(char x)
- {
- if ( isdigit(x) )
- return (int)(x-'0');
- else return 10+(int)(x-'A');
- }
- int main()
- {
- freopen("code.in", "r", stdin);
- freopen("code.out", "w", stdout);
- scanf("%d %d%*c", &k, &n);
- scanf("%s", s);
- k--;
- r=-1;
- int l =0;
- //while ( l<n && s[l] == '0') l++;
- for (int i=n-1; i>=l; i--)
- {
- int t = chislo(s[i]);
- if ( t<k)
- {
- if ( r != -1)
- {
- t++;
- sort(s+i+1, s+n);
- for (int j=i+1; j<n; j++)
- if ( s[j] != '0' )
- {
- r=j;
- break;
- }
- int t2 = chislo(s[r]);
- t2--;
- if ( t < 10)
- s[i] = (char)('0'+t);
- else
- {
- t-=10;
- s[i] = (char)('A'+t);
- }
- if ( t2 < 10)
- s[r] = (char)('0'+t2);
- else
- {
- t2-=10;
- s[r] = (char)('A'+t2);
- }
- sort(s+i+1, s+n);
- printf("%s", s);
- return 0;
- }
- }
- if ( t > 0)
- r = i;
- }
- printf("Impossible");
- }
Advertisement
Add Comment
Please, Sign In to add comment